1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include "background.h"
#include "camera.h"
#include "device.h"
#include "film.h"
#include "filter.h"
#include "integrator.h"
#include "light.h"
#include "shader.h"
#include "mesh.h"
#include "object.h"
#include "particles.h"
#include "curves.h"
#include "scene.h"
#include "svm.h"
#include "osl.h"
#include "util_foreach.h"
#include "util_progress.h"
CCL_NAMESPACE_BEGIN
Scene::Scene(const SceneParams& params_, const DeviceInfo& device_info_)
: params(params_)
{
device = NULL;
memset(&dscene.data, 0, sizeof(dscene.data));
camera = new Camera();
filter = new Filter();
film = new Film();
background = new Background();
light_manager = new LightManager();
mesh_manager = new MeshManager();
object_manager = new ObjectManager();
integrator = new Integrator();
image_manager = new ImageManager();
particle_system_manager = new ParticleSystemManager();
curve_system_manager = new CurveSystemManager();
/* OSL only works on the CPU */
if(device_info_.type == DEVICE_CPU)
shader_manager = ShaderManager::create(this, params.shadingsystem);
else
shader_manager = ShaderManager::create(this, SceneParams::SVM);
if (device_info_.type == DEVICE_CPU)
image_manager->set_extended_image_limits();
}
Scene::~Scene()
{
free_memory(true);
}
void Scene::free_memory(bool final)
{
foreach(Shader *s, shaders)
delete s;
foreach(Mesh *m, meshes)
delete m;
foreach(Object *o, objects)
delete o;
foreach(Light *l, lights)
delete l;
foreach(ParticleSystem *p, particle_systems)
delete p;
shaders.clear();
meshes.clear();
objects.clear();
lights.clear();
particle_systems.clear();
if(device) {
camera->device_free(device, &dscene);
filter->device_free(device, &dscene);
film->device_free(device, &dscene);
background->device_free(device, &dscene);
integrator->device_free(device, &dscene);
object_manager->device_free(device, &dscene);
mesh_manager->device_free(device, &dscene);
shader_manager->device_free(device, &dscene);
light_manager->device_free(device, &dscene);
particle_system_manager->device_free(device, &dscene);
curve_system_manager->device_free(device, &dscene);
if(!params.persistent_data || final)
image_manager->device_free(device, &dscene);
}
if(final) {
delete filter;
delete camera;
delete film;
delete background;
delete integrator;
delete object_manager;
delete mesh_manager;
delete shader_manager;
delete light_manager;
delete particle_system_manager;
delete curve_system_manager;
delete image_manager;
}
}
void Scene::device_update(Device *device_, Progress& progress)
{
if(!device)
device = device_;
/* The order of updates is important, because there's dependencies between
* the different managers, using data computed by previous managers.
*
* - Background generates shader graph compiled by shader manager.
* - Image manager uploads images used by shaders.
* - Camera may be used for adapative subdivison.
* - Displacement shader must have all shader data available.
* - Light manager needs final mesh data to compute emission CDF.
*/
image_manager->set_pack_images(device->info.pack_images);
progress.set_status("Updating Background");
background->device_update(device, &dscene, this);
if(progress.get_cancel()) return;
progress.set_status("Updating Shaders");
shader_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Images");
image_manager->device_update(device, &dscene, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Camera");
camera->device_update(device, &dscene, this);
if(progress.get_cancel()) return;
progress.set_status("Updating Objects");
object_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Hair Systems");
curve_system_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Meshes");
mesh_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Lights");
light_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Particle Systems");
particle_system_manager->device_update(device, &dscene, this, progress);
if(progress.get_cancel()) return;
progress.set_status("Updating Filter");
filter->device_update(device, &dscene);
if(progress.get_cancel()) return;
progress.set_status("Updating Film");
film->device_update(device, &dscene);
if(progress.get_cancel()) return;
progress.set_status("Updating Integrator");
integrator->device_update(device, &dscene, this);
if(progress.get_cancel()) return;
progress.set_status("Updating Device", "Writing constant memory");
device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
}
Scene::MotionType Scene::need_motion(bool advanced_shading)
{
if(integrator->motion_blur)
return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
else if(Pass::contains(film->passes, PASS_MOTION))
return MOTION_PASS;
else
return MOTION_NONE;
}
bool Scene::need_global_attribute(AttributeStandard std)
{
if(std == ATTR_STD_UV)
return Pass::contains(film->passes, PASS_UV);
if(std == ATTR_STD_MOTION_PRE || ATTR_STD_MOTION_POST)
return need_motion() == MOTION_PASS;
return false;
}
void Scene::need_global_attributes(AttributeRequestSet& attributes)
{
for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
if(need_global_attribute((AttributeStandard)std))
attributes.add((AttributeStandard)std);
}
bool Scene::need_update()
{
return (need_reset() || film->need_update);
}
bool Scene::need_reset()
{
return (background->need_update
|| image_manager->need_update
|| camera->need_update
|| object_manager->need_update
|| mesh_manager->need_update
|| light_manager->need_update
|| filter->need_update
|| integrator->need_update
|| shader_manager->need_update
|| particle_system_manager->need_update
|| curve_system_manager->need_update);
}
void Scene::reset()
{
shader_manager->reset(this);
shader_manager->add_default(this);
/* ensure all objects are updated */
camera->tag_update();
filter->tag_update(this);
film->tag_update(this);
background->tag_update(this);
integrator->tag_update(this);
}
void Scene::device_free()
{
free_memory(false);
}
CCL_NAMESPACE_END
|