Welcome to mirror list, hosted at ThFree Co, Russian Federation.

volume.cpp « render « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7d1d7d6595ac389042f83ca81175d967cd49fd5 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright 2015 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "scene.h"
#include "volume.h"

#include "util/util_foreach.h"
#include "util/util_logging.h"
#include "util/util_progress.h"
#include "util/util_task.h"

#include "../kernel/openvdb/vdb_globals.h"

CCL_NAMESPACE_BEGIN

#define MAX_VOLUME 1024

void Volume::tag_update(Scene *scene, bool /*rebuild*/)
{
	scene->volume_manager->need_update = true;
}

/* ------------------------------------------------------------------------- */

VolumeManager::VolumeManager()
{
#ifdef WITH_OPENVDB
	openvdb::initialize();

	current_grids.reserve(64);
#endif

	need_update = true;
	num_float_volume = 0;
	num_float3_volume = 0;
}

VolumeManager::~VolumeManager()
{
	current_grids.clear();
}

static inline void catch_exceptions()
{
#ifdef WITH_OPENVDB
	try {
		throw;
	}
	catch(const openvdb::IoError& e) {
		std::cerr << e.what() << "\n";
	}
#endif
}

int VolumeManager::add_volume(Volume *volume, const std::string &filename, const std::string &name)
{
	size_t slot = -1;

	if((slot = find_existing_slot(volume, filename, name)) != -1) {
		return slot;
	}

	if((num_float_volume + num_float3_volume + 1) > MAX_VOLUME) {
		printf("VolumeManager::add_volume: volume limit reached %d!\n", MAX_VOLUME);
		return -1;
	}

	try {
		if(is_openvdb_file(filename)) {
			slot = add_openvdb_volume(volume, filename, name);
		}

		add_grid_description(volume, filename, name, slot);

		volumes.push_back(volume);
	}
	catch(...) {
		catch_exceptions();
		slot = -1;
	}

	return slot;
}

int VolumeManager::find_existing_slot(Volume *volume, const std::string &filename, const std::string &name)
{
	for(size_t i = 0; i < current_grids.size(); ++i) {
		GridDescription grid = current_grids[i];

		if(grid.volume == volume) {
			if(grid.filename == filename && grid.name == name) {
				return grid.slot;
			}
		}
	}

	return -1;
}

int VolumeManager::find_density_slot()
{
	/* first try finding a matching grid name */
	for(size_t i = 0; i < current_grids.size(); ++i) {
		GridDescription grid = current_grids[i];
		
		if(string_iequals(grid.name, "density") || string_iequals(grid.name, "density high"))
			return grid.slot;
	}
	
	/* try using the first scalar float grid instead */
	for (size_t i = 0; i < volumes.size(); ++i) {
		Volume *volume = volumes[i];
		
		if (!volume->scalar_grids.empty()) {
			return 0;
		}
	}
	
	return -1;
}

bool VolumeManager::is_openvdb_file(const string& filename) const
{
	return string_endswith(filename, ".vdb");
}

template <typename Container>
size_t find_empty_slot(Container container)
{
	size_t slot = 0;

	for(; slot < container.size(); ++slot) {
		if(!container[slot]) {
			break;
		}
	}

	if(slot == container.size()) {
		if(slot == MAX_VOLUME) {
			printf("VolumeManager::add_volume: volume limit reached %d!\n",
			       MAX_VOLUME);
			return -1;
		}

		container.resize(slot + 1);
	}

	return slot;
}

size_t VolumeManager::add_openvdb_volume(Volume *volume, const std::string &filename, const std::string &name)
{
	size_t slot = -1;

#ifdef WITH_OPENVDB
	openvdb::io::File file(filename);
	file.open();

	if(!file.hasGrid(name)) return -1;

	openvdb::GridBase::Ptr grid = file.readGrid(name);
	if(grid->getGridClass() == openvdb::GRID_LEVEL_SET) return -1;

	if(grid->isType<openvdb::FloatGrid>()) {
		openvdb::FloatGrid::Ptr fgrid = openvdb::gridPtrCast<openvdb::FloatGrid>(grid);
		volume->scalar_grids.push_back(fgrid);

		/* XXX Ray intersectors only support uniform grids.
		 * Can we make this transparent somehow? - lukas
		 */
		assert(fgrid->hasUniformVoxels());

		slot = num_float_volume++;
	}
	else if(grid->isType<openvdb::Vec3SGrid>()) {
		openvdb::Vec3SGrid::Ptr vgrid = openvdb::gridPtrCast<openvdb::Vec3SGrid>(grid);
		volume->vector_grids.push_back(vgrid);

		slot = num_float3_volume++;
	}
#else
	(void)volume;
	(void)filename;
	(void)name;
#endif

	return slot;
}

void VolumeManager::add_grid_description(Volume *volume, const std::string &filename, const std::string &name, int slot)
{
	GridDescription descr;
	descr.filename = filename;
	descr.name = name;
	descr.volume = volume;
	descr.slot = slot;

	current_grids.push_back(descr);
}

static void update_attribute_element_offset(Attribute *vattr,
                                            TypeDesc& type,
                                            int& offset,
                                            AttributeElement& element)
{
	if(vattr) {
		/* store element and type */
		element = vattr->element;
		type = vattr->type;

		/* store slot in offset value */
		VoxelAttribute *voxel_data = vattr->data_voxel();
		offset = voxel_data->slot;
	}
	else {
		/* attribute not found */
		element = ATTR_ELEMENT_NONE;
		offset = 0;
	}
}

void VolumeManager::device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
	progress.set_status("Updating Volume", "Computing attributes");

	/* gather per volume requested attributes. as volumes may have multiple
	 * shaders assigned, this merges the requested attributes that have
	 * been set per shader by the shader manager */
	vector<AttributeRequestSet> volume_attributes(volumes.size());

	for(size_t i = 0; i < volumes.size(); i++) {
		Volume *volume = volumes[i];

		foreach(Shader *shader, volume->used_shaders) {
			volume_attributes[i].add(shader->attributes);
		}
	}

	for(size_t i = 0; i < volumes.size(); i++) {
		Volume *volume = volumes[i];
		AttributeRequestSet& attributes = volume_attributes[i];

		/* todo: we now store std and name attributes from requests even if
		 * they actually refer to the same mesh attributes, optimize */
		foreach(AttributeRequest& req, attributes.requests) {
			Attribute *vattr = volume->attributes.find(req);

			update_attribute_element_offset(vattr,
			                                req.triangle_type,
			                                req.triangle_desc.offset,
			                                req.triangle_desc.element);

			if(progress.get_cancel()) return;
		}
	}

	update_svm_attributes(device, dscene, scene, volume_attributes);
}

void VolumeManager::update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
{
	/* compute array stride */
	int attr_map_stride = 0;

	for(size_t i = 0; i < volumes.size(); i++) {
		attr_map_stride = max(attr_map_stride, (mesh_attributes[i].size() + 1));
	}

	if(attr_map_stride == 0) {
		return;
	}

	/* create attribute map */
	uint4 *attr_map = dscene->attributes_map.resize(attr_map_stride*volumes.size());
	memset(attr_map, 0, dscene->attributes_map.size()*sizeof(uint));

	for(size_t i = 0; i < volumes.size(); i++) {
		AttributeRequestSet& attributes = mesh_attributes[i];

		/* set object attributes */
		int index = i*attr_map_stride;

		foreach(AttributeRequest& req, attributes.requests) {
			uint id = scene->shader_manager->get_attribute_id(req.name);

			attr_map[index].x = id;
			attr_map[index].y = req.triangle_desc.element;
			attr_map[index].z = as_uint(req.triangle_desc.offset);

			if(req.triangle_type == TypeDesc::TypeFloat)
				attr_map[index].w = NODE_ATTR_FLOAT;
			else
				attr_map[index].w = NODE_ATTR_FLOAT3;

			index++;
		}

		/* terminator */
		attr_map[index].x = ATTR_STD_NONE;
		attr_map[index].y = 0;
		attr_map[index].z = 0;
		attr_map[index].w = 0;

		index++;
	}

	device->tex_alloc("__attributes_map", dscene->attributes_map);
}

void VolumeManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
	if(!need_update) {
		return;
	}

	device_free(device, dscene);
	progress.set_status("Updating OpenVDB volumes", "Sending volumes to device.");

	uint *vol_shader = dscene->vol_shader.resize(num_float_volume + num_float3_volume);
	int s = 0;

	for (size_t i = 0; i < volumes.size(); ++i) {
		Volume *volume = volumes[i];

		for(size_t i = 0; i < volume->scalar_grids.size(); ++i) {
			if(!volume->scalar_grids[i]) {
				continue;
			}

			vol_shader[s++] = scene->shader_manager->get_shader_id(volume->used_shaders[0], false);
		}

		for(size_t i = 0; i < volume->vector_grids.size(); ++i) {
			if(!volume->vector_grids[i]) {
				continue;
			}

			vol_shader[s++] = scene->shader_manager->get_shader_id(volume->used_shaders[0], false);
		}

		if(progress.get_cancel()) {
			return;
		}
	}

	device->tex_alloc("__vol_shader", dscene->vol_shader);

#ifdef WITH_OPENVDB
	typedef typename OpenVDBGlobals::scalar_grid_t scalar_grid_t;
	typedef typename OpenVDBGlobals::vector_grid_t vector_grid_t;
	typedef typename OpenVDBGlobals::scalar_isector_t scalar_isector_t;
	typedef typename OpenVDBGlobals::vector_isector_t vector_isector_t;
	OpenVDBGlobals *vdb = device->vdb_memory();
	
	vdb->scalar_grids.reserve(num_float_volume);
	vdb->vector_grids.reserve(num_float3_volume);
	vdb->scalar_main_isectors.reserve(num_float_volume);
	vdb->vector_main_isectors.reserve(num_float3_volume);
	for (size_t i = 0; i < volumes.size(); ++i) {
		Volume *volume = volumes[i];
		
		for (size_t k = 0; k < volume->scalar_grids.size(); ++k) {
			scalar_grid_t *grid = volume->scalar_grids[k].get();
			vdb->scalar_grids.push_back(grid);
			vdb->scalar_main_isectors.push_back(new scalar_isector_t(*grid));
			VLOG(1) << grid->getName().c_str() << " memory usage: " << grid->memUsage() / 1024.0f << " kilobytes.\n";
		}
		for (size_t k = 0; k < volume->vector_grids.size(); ++k) {
			vector_grid_t *grid = volume->vector_grids[k].get();
			vdb->vector_grids.push_back(grid);
			vdb->vector_main_isectors.push_back(new vector_isector_t(*grid));
			VLOG(1) << grid->getName().c_str() << " memory usage: " << grid->memUsage() / 1024.0f << " kilobytes.\n";
		}
	}
#endif

	if(progress.get_cancel()) {
		return;
	}

	dscene->data.tables.num_volumes = num_float_volume/* + float3_volumes.size()*/;
	dscene->data.tables.density_index = 0;

	need_update = false;
}

void VolumeManager::device_free(Device *device, DeviceScene *dscene)
{
#ifdef WITH_OPENVDB
	OpenVDBGlobals *vdb = device->vdb_memory();
	for (size_t i = 0; i < vdb->scalar_main_isectors.size(); ++i) {
		delete vdb->scalar_main_isectors[i];
	}
	vdb->scalar_grids.clear();
	vdb->scalar_main_isectors.clear();
	
	for (size_t i = 0; i < vdb->vector_main_isectors.size(); ++i) {
		delete vdb->vector_main_isectors[i];
	}
	vdb->vector_grids.clear();
	vdb->vector_main_isectors.clear();
#endif
	
	device->tex_free(dscene->vol_shader);
	dscene->vol_shader.clear();
}

void VolumeManager::tag_update(Scene */*scene*/)
{
	need_update = true;
}

CCL_NAMESPACE_END