Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / render / scene.h
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/image.h"
23 #include "render/shader.h"
24
25 #include "device/device_memory.h"
26
27 #include "util/util_param.h"
28 #include "util/util_string.h"
29 #include "util/util_system.h"
30 #include "util/util_texture.h"
31 #include "util/util_thread.h"
32 #include "util/util_types.h"
33 #include "util/util_vector.h"
34
35 CCL_NAMESPACE_BEGIN
36
37 class AttributeRequestSet;
38 class Background;
39 class Camera;
40 class Device;
41 class DeviceInfo;
42 class Film;
43 class Integrator;
44 class Light;
45 class LightManager;
46 class LookupTables;
47 class Mesh;
48 class MeshManager;
49 class Object;
50 class ObjectManager;
51 class ParticleSystemManager;
52 class ParticleSystem;
53 class CurveSystemManager;
54 class Shader;
55 class ShaderManager;
56 class Progress;
57 class BakeManager;
58 class BakeData;
59 class RenderStats;
60
61 /* Scene Device Data */
62
63 class DeviceScene {
64 public:
65         /* BVH */
66         device_vector<int4> bvh_nodes;
67         device_vector<int4> bvh_leaf_nodes;
68         device_vector<int> object_node;
69         device_vector<uint> prim_tri_index;
70         device_vector<float4> prim_tri_verts;
71         device_vector<int> prim_type;
72         device_vector<uint> prim_visibility;
73         device_vector<int> prim_index;
74         device_vector<int> prim_object;
75         device_vector<float2> prim_time;
76
77         /* mesh */
78         device_vector<uint> tri_shader;
79         device_vector<float4> tri_vnormal;
80         device_vector<uint4> tri_vindex;
81         device_vector<uint> tri_patch;
82         device_vector<float2> tri_patch_uv;
83
84         device_vector<float4> curves;
85         device_vector<float4> curve_keys;
86
87         device_vector<uint> patches;
88
89         /* objects */
90         device_vector<KernelObject> objects;
91         device_vector<Transform> object_motion_pass;
92         device_vector<DecomposedTransform> object_motion;
93         device_vector<uint> object_flag;
94
95         /* cameras */
96         device_vector<DecomposedTransform> camera_motion;
97
98         /* attributes */
99         device_vector<uint4> attributes_map;
100         device_vector<float> attributes_float;
101         device_vector<float4> attributes_float3;
102         device_vector<uchar4> attributes_uchar4;
103
104         /* lights */
105         device_vector<KernelLightDistribution> light_distribution;
106         device_vector<KernelLight> lights;
107         device_vector<float2> light_background_marginal_cdf;
108         device_vector<float2> light_background_conditional_cdf;
109
110         /* particles */
111         device_vector<KernelParticle> particles;
112
113         /* shaders */
114         device_vector<int4> svm_nodes;
115         device_vector<KernelShader> shaders;
116
117         /* lookup tables */
118         device_vector<float> lookup_table;
119
120         /* integrator */
121         device_vector<uint> sobol_directions;
122
123         /* ies lights */
124         device_vector<float> ies_lights;
125
126         KernelData data;
127
128         DeviceScene(Device *device);
129 };
130
131 /* Scene Parameters */
132
133 class SceneParams {
134 public:
135         /* Type of BVH, in terms whether it is supported dynamic updates of meshes
136          * or whether modifying geometry requires full BVH rebuild.
137          */
138         enum BVHType {
139                 /* BVH supports dynamic updates of geometry.
140                  *
141                  * Faster for updating BVH tree when doing modifications in viewport,
142                  * but slower for rendering.
143                  */
144                 BVH_DYNAMIC = 0,
145                 /* BVH tree is calculated for specific scene, updates in geometry
146                  * requires full tree rebuild.
147                  *
148                  * Slower to update BVH tree when modifying objects in viewport, also
149                  * slower to build final BVH tree but gives best possible render speed.
150                  */
151                 BVH_STATIC = 1,
152
153                 BVH_NUM_TYPES,
154         };
155
156         ShadingSystem shadingsystem;
157
158         /* Requested BVH layout.
159          *
160          * If it's not supported by the device, the widest one from supported ones
161          * will be used, but BVH wider than this one will never be used.
162          */
163         BVHLayout bvh_layout;
164
165         BVHType bvh_type;
166         bool use_bvh_spatial_split;
167         bool use_bvh_unaligned_nodes;
168         int num_bvh_time_steps;
169         bool persistent_data;
170         int texture_limit;
171
172         SceneParams()
173         {
174                 shadingsystem = SHADINGSYSTEM_SVM;
175                 bvh_layout = BVH_LAYOUT_BVH2;
176                 bvh_type = BVH_DYNAMIC;
177                 use_bvh_spatial_split = false;
178                 use_bvh_unaligned_nodes = true;
179                 num_bvh_time_steps = 0;
180                 persistent_data = false;
181                 texture_limit = 0;
182         }
183
184         bool modified(const SceneParams& params)
185         { return !(shadingsystem == params.shadingsystem
186                 && bvh_layout == params.bvh_layout
187                 && bvh_type == params.bvh_type
188                 && use_bvh_spatial_split == params.use_bvh_spatial_split
189                 && use_bvh_unaligned_nodes == params.use_bvh_unaligned_nodes
190                 && num_bvh_time_steps == params.num_bvh_time_steps
191                 && persistent_data == params.persistent_data
192                 && texture_limit == params.texture_limit); }
193 };
194
195 /* Scene */
196
197 class Scene {
198 public:
199         /* Optional name. Is used for logging and reporting. */
200         string name;
201
202         /* data */
203         Camera *camera;
204         Camera *dicing_camera;
205         LookupTables *lookup_tables;
206         Film *film;
207         Background *background;
208         Integrator *integrator;
209
210         /* data lists */
211         vector<Object*> objects;
212         vector<Mesh*> meshes;
213         vector<Shader*> shaders;
214         vector<Light*> lights;
215         vector<ParticleSystem*> particle_systems;
216
217         /* data managers */
218         ImageManager *image_manager;
219         LightManager *light_manager;
220         ShaderManager *shader_manager;
221         MeshManager *mesh_manager;
222         ObjectManager *object_manager;
223         ParticleSystemManager *particle_system_manager;
224         CurveSystemManager *curve_system_manager;
225         BakeManager *bake_manager;
226
227         /* default shaders */
228         Shader *default_surface;
229         Shader *default_light;
230         Shader *default_background;
231         Shader *default_empty;
232
233         /* device */
234         Device *device;
235         DeviceScene dscene;
236
237         /* parameters */
238         SceneParams params;
239
240         /* mutex must be locked manually by callers */
241         thread_mutex mutex;
242
243         Scene(const SceneParams& params, Device *device);
244         ~Scene();
245
246         void device_update(Device *device, Progress& progress);
247
248         bool need_global_attribute(AttributeStandard std);
249         void need_global_attributes(AttributeRequestSet& attributes);
250
251         enum MotionType { MOTION_NONE = 0, MOTION_PASS, MOTION_BLUR };
252         MotionType need_motion();
253         float motion_shutter_time();
254
255         bool need_update();
256         bool need_reset();
257
258         void reset();
259         void device_free();
260
261         void collect_statistics(RenderStats *stats);
262
263 protected:
264         /* Check if some heavy data worth logging was updated.
265          * Mainly used to suppress extra annoying logging.
266          */
267         bool need_data_update();
268
269         void free_memory(bool final);
270 };
271
272 CCL_NAMESPACE_END
273
274 #endif  /*  __SCENE_H__ */