Cycles: fix AO pass not rendering with AO enabled for world, and make mask
[blender.git] / intern / cycles / render / scene.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdlib.h>
20
21 #include "background.h"
22 #include "camera.h"
23 #include "device.h"
24 #include "film.h"
25 #include "filter.h"
26 #include "integrator.h"
27 #include "light.h"
28 #include "shader.h"
29 #include "mesh.h"
30 #include "object.h"
31 #include "scene.h"
32 #include "svm.h"
33 #include "osl.h"
34
35 #include "util_foreach.h"
36 #include "util_progress.h"
37
38 CCL_NAMESPACE_BEGIN
39
40 Scene::Scene(const SceneParams& params_)
41 : params(params_)
42 {
43         device = NULL;
44         memset(&dscene.data, 0, sizeof(dscene.data));
45
46         camera = new Camera();
47         filter = new Filter();
48         film = new Film();
49         background = new Background();
50         light_manager = new LightManager();
51         mesh_manager = new MeshManager();
52         object_manager = new ObjectManager();
53         integrator = new Integrator();
54         image_manager = new ImageManager();
55         shader_manager = ShaderManager::create(this);
56 }
57
58 Scene::~Scene()
59 {
60         if(device) camera->device_free(device, &dscene);
61         delete camera;
62
63         if(device) filter->device_free(device, &dscene);
64         delete filter;
65
66         if(device) film->device_free(device, &dscene);
67         delete film;
68
69         if(device) background->device_free(device, &dscene);
70         delete background;
71
72         if(device) mesh_manager->device_free(device, &dscene);
73         delete mesh_manager;
74
75         if(device) object_manager->device_free(device, &dscene);
76         delete object_manager;
77
78         if(device) integrator->device_free(device, &dscene);
79         delete integrator;
80
81         if(device) shader_manager->device_free(device, &dscene);
82         delete shader_manager;
83
84         if(device) light_manager->device_free(device, &dscene);
85         delete light_manager;
86
87         foreach(Shader *s, shaders)
88                 delete s;
89         foreach(Mesh *m, meshes)
90                 delete m;
91         foreach(Object *o, objects)
92                 delete o;
93         foreach(Light *l, lights)
94                 delete l;
95
96         if(device) image_manager->device_free(device, &dscene);
97         delete image_manager;
98 }
99
100 void Scene::device_update(Device *device_, Progress& progress)
101 {
102         if(!device)
103                 device = device_;
104         
105         /* The order of updates is important, because there's dependencies between
106          * the different managers, using data computed by previous managers.
107          *
108          * - Background generates shader graph compiled by shader manager.
109          * - Image manager uploads images used by shaders.
110          * - Camera may be used for adapative subdivison.
111          * - Displacement shader must have all shader data available.
112          * - Light manager needs final mesh data to compute emission CDF.
113          */
114
115         progress.set_status("Updating Background");
116         background->device_update(device, &dscene, this);
117
118         if(progress.get_cancel()) return;
119
120         progress.set_status("Updating Shaders");
121         shader_manager->device_update(device, &dscene, this, progress);
122
123         if(progress.get_cancel()) return;
124
125         progress.set_status("Updating Images");
126         image_manager->device_update(device, &dscene, progress);
127
128         if(progress.get_cancel()) return;
129
130         progress.set_status("Updating Camera");
131         camera->device_update(device, &dscene);
132
133         if(progress.get_cancel()) return;
134
135         progress.set_status("Updating Objects");
136         object_manager->device_update(device, &dscene, this, progress);
137
138         if(progress.get_cancel()) return;
139
140         progress.set_status("Updating Meshes");
141         mesh_manager->device_update(device, &dscene, this, progress);
142
143         if(progress.get_cancel()) return;
144
145         progress.set_status("Updating Lights");
146         light_manager->device_update(device, &dscene, this, progress);
147
148         if(progress.get_cancel()) return;
149
150         progress.set_status("Updating Filter");
151         filter->device_update(device, &dscene);
152
153         if(progress.get_cancel()) return;
154
155         progress.set_status("Updating Film");
156         film->device_update(device, &dscene);
157
158         if(progress.get_cancel()) return;
159
160         progress.set_status("Updating Integrator");
161         integrator->device_update(device, &dscene);
162
163         if(progress.get_cancel()) return;
164
165         progress.set_status("Updating Device", "Writing constant memory");
166         device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
167 }
168
169 bool Scene::need_update()
170 {
171         return (need_reset() || film->need_update);
172 }
173
174 bool Scene::need_reset()
175 {
176         return (background->need_update
177                 || image_manager->need_update
178                 || camera->need_update
179                 || object_manager->need_update
180                 || mesh_manager->need_update
181                 || light_manager->need_update
182                 || filter->need_update
183                 || integrator->need_update
184                 || shader_manager->need_update);
185 }
186
187 CCL_NAMESPACE_END
188