[Bf-blender-cvs] [27c70e50f78] soc-2018-cycles-volumes: Clean up. Change VDB iteration method in Cycles to leaf iterators rather than accessors.

Geraldine Chua noreply at git.blender.org
Sat Jul 28 18:02:38 CEST 2018


Commit: 27c70e50f783909fc46ac04fdfa955a6f0bbd76d
Author: Geraldine Chua
Date:   Sun Jul 29 00:01:48 2018 +0800
Branches: soc-2018-cycles-volumes
https://developer.blender.org/rB27c70e50f783909fc46ac04fdfa955a6f0bbd76d

Clean up. Change VDB iteration method in Cycles to leaf iterators rather than accessors.

===================================================================

M	intern/cycles/device/device_cpu.cpp
M	intern/cycles/device/device_memory.cpp
M	intern/cycles/device/device_memory.h
M	intern/cycles/device/device_memory_openvdb.h
M	intern/cycles/render/image.cpp
M	intern/cycles/render/image.h
M	intern/cycles/render/mesh_volume.cpp
M	intern/cycles/render/openvdb.cpp
M	intern/cycles/render/openvdb.h
M	intern/cycles/util/util_sparse_grid.h

===================================================================

diff --git a/intern/cycles/device/device_cpu.cpp b/intern/cycles/device/device_cpu.cpp
index f29b54ff237..0b8e95c03c9 100644
--- a/intern/cycles/device/device_cpu.cpp
+++ b/intern/cycles/device/device_cpu.cpp
@@ -377,9 +377,11 @@ public:
 	void tex_alloc(device_memory& mem)
 	{
 		size_t total_memory = mem.memory_size();
-		device_memory *grid_info = mem.grid_info;
-		if(grid_info) {
-			total_memory += grid_info->memory_size();
+		device_memory *sparse_mem = NULL;
+
+		if(mem.grid_info && mem.grid_type == IMAGE_GRID_TYPE_SPARSE) {
+			sparse_mem = (device_memory*)mem.grid_info;
+			total_memory += sparse_mem->memory_size();
 		}
 
 		VLOG(1) << "Texture allocate: " << mem.name << ", "
@@ -420,16 +422,12 @@ public:
 			info.height = mem.real_height;
 			info.depth = mem.real_depth;
 
-			/* For OpenVDB textures, the kernel will retrieve the accessor from
-			 * util, but there must be some value stored in data or the texture
-			 * will not be used. As a stopgap, the accessor pointer will just
-			 * be stored in both data and util. */
 			switch(mem.grid_type) {
 				case IMAGE_GRID_TYPE_OPENVDB:
-					info.util = info.data;
+					info.util = (uint64_t)mem.grid_info;
 					break;
 				case IMAGE_GRID_TYPE_SPARSE:
-					info.util = (uint64_t)grid_info->host_pointer;
+					info.util = (uint64_t)sparse_mem->host_pointer;
 					info.tiled_width = get_tile_res(info.width);
 					info.tiled_height = get_tile_res(info.height);
 					info.even_width = info.width - (info.width % TILE_SIZE);
@@ -449,18 +447,19 @@ public:
 		mem.device_size = mem.memory_size();
 		stats.mem_alloc(mem.device_size);
 
-		if(grid_info) {
-			grid_info->device_pointer = (device_ptr)grid_info->host_pointer;
-			grid_info->device_size = grid_info->memory_size();
-			stats.mem_alloc(grid_info->device_size);
+		if(sparse_mem) {
+			sparse_mem->device_pointer = (device_ptr)sparse_mem->host_pointer;
+			sparse_mem->device_size = sparse_mem->memory_size();
+			stats.mem_alloc(sparse_mem->device_size);
 		}
 	}
 
 	void tex_free(device_memory& mem)
 	{
 		if(mem.device_pointer) {
-			if(mem.grid_info) {
-				tex_free(*mem.grid_info);
+			if(mem.grid_info && mem.grid_type == IMAGE_GRID_TYPE_SPARSE) {
+				device_memory *grid_info = (device_memory*)mem.grid_info;
+				tex_free(*grid_info);
 			}
 			mem.device_pointer = 0;
 			stats.mem_free(mem.device_size);
diff --git a/intern/cycles/device/device_memory.cpp b/intern/cycles/device/device_memory.cpp
index c6248fcf88b..6e5ded19699 100644
--- a/intern/cycles/device/device_memory.cpp
+++ b/intern/cycles/device/device_memory.cpp
@@ -36,7 +36,8 @@ device_memory::device_memory(Device *device, const char *name, MemoryType type)
   device(device),
   device_pointer(0),
   host_pointer(0),
-  shared_pointer(0)
+  shared_pointer(0),
+  grid_info(0)
 {
 }
 
diff --git a/intern/cycles/device/device_memory.h b/intern/cycles/device/device_memory.h
index e21636009ee..5d4fb130ca2 100644
--- a/intern/cycles/device/device_memory.h
+++ b/intern/cycles/device/device_memory.h
@@ -207,7 +207,7 @@ public:
 	device_ptr device_pointer;
 	void *host_pointer;
 	void *shared_pointer;
-	device_memory *grid_info = NULL;
+	void *grid_info;
 
 	virtual ~device_memory();
 
diff --git a/intern/cycles/device/device_memory_openvdb.h b/intern/cycles/device/device_memory_openvdb.h
index eb9fbe4912b..ee655318b61 100644
--- a/intern/cycles/device/device_memory_openvdb.h
+++ b/intern/cycles/device/device_memory_openvdb.h
@@ -41,7 +41,8 @@ public:
 
 		assert(data_elements > 0);
 
-		host_pointer = static_cast<void*>(&vdb_acc);
+		host_pointer = static_cast<void*>(&vdb_grid);
+		grid_info = static_cast<void*>(&vdb_acc);
 
 		data_width = real_width = resolution.x;
 		data_height = real_height = resolution.y;
diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index 13892db1364..8c79d245614 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -474,6 +474,33 @@ void ImageManager::tag_reload_image(const string& filename,
 	}
 }
 
+bool ImageManager::allocate_sparse_index(Device *device,
+                                         device_memory *tex_img,
+                                         vector<int> *sparse_index,
+                                         string mem_name)
+{
+	mem_name += "_index";
+	device_vector<int> *tex_index =
+	        new device_vector<int>(device, mem_name.c_str(), MEM_TEXTURE);
+
+	int *ti;
+	{
+		thread_scoped_lock device_lock(device_mutex);
+		ti = (int*)tex_index->alloc(sparse_index->size());
+	}
+
+	if(ti == NULL) {
+		return false;
+	}
+
+	memcpy(ti, &(*sparse_index)[0], sparse_index->size() * sizeof(int));
+
+	tex_img->grid_info = static_cast<void*>(tex_index);
+	tex_img->grid_type = IMAGE_GRID_TYPE_SPARSE;
+
+	return true;
+}
+
 bool ImageManager::file_load_image_generic(Image *img,
                                            ImageInput **in,
                                            int &width,
@@ -565,8 +592,7 @@ bool ImageManager::file_load_image_generic(Image *img,
 	return true;
 }
 
-template<typename StorageType,
-         typename DeviceType>
+template<typename DeviceType>
 void ImageManager::file_load_failed(Image *img,
                                     ImageDataType type,
 			                        device_vector<DeviceType> *tex_img)
@@ -577,36 +603,54 @@ void ImageManager::file_load_failed(Image *img,
 
 	/* On failure to load, we set a 1x1 pixels pink image. */
 	thread_scoped_lock device_lock(device_mutex);
-	StorageType *pixels = (StorageType*)tex_img->alloc(1, 1);
+	DeviceType *device_pixels = tex_img->alloc(1, 1);
 
 	switch(type) {
 		case IMAGE_DATA_TYPE_FLOAT4:
-			pixels[0] = TEX_IMAGE_MISSING_R;
-			pixels[1] = TEX_IMAGE_MISSING_G;
-			pixels[2] = TEX_IMAGE_MISSING_B;
-			pixels[3] = TEX_IMAGE_MISSING_A;
+		{
+			float4 *pixels = (float4*)device_pixels;
+			pixels[0].x = TEX_IMAGE_MISSING_R;
+			pixels[0].y = TEX_IMAGE_MISSING_G;
+			pixels[0].z = TEX_IMAGE_MISSING_B;
+			pixels[0].w = TEX_IMAGE_MISSING_A;
 			break;
+		}
 		case IMAGE_DATA_TYPE_FLOAT:
+		{
+			float *pixels = (float*)device_pixels;
 			pixels[0] = TEX_IMAGE_MISSING_R;
 			break;
+		}
 		case IMAGE_DATA_TYPE_BYTE4:
-			pixels[0] = (TEX_IMAGE_MISSING_R * 255);
-			pixels[1] = (TEX_IMAGE_MISSING_G * 255);
-			pixels[2] = (TEX_IMAGE_MISSING_B * 255);
-			pixels[3] = (TEX_IMAGE_MISSING_A * 255);
+		{
+			uchar4 *pixels = (uchar4*)device_pixels;
+			pixels[0].x = (TEX_IMAGE_MISSING_R * 255);
+			pixels[0].y = (TEX_IMAGE_MISSING_G * 255);
+			pixels[0].z = (TEX_IMAGE_MISSING_B * 255);
+			pixels[0].w = (TEX_IMAGE_MISSING_A * 255);
 			break;
+		}
 		case IMAGE_DATA_TYPE_BYTE:
+		{
+			uchar *pixels = (uchar*)device_pixels;
 			pixels[0] = (TEX_IMAGE_MISSING_R * 255);
 			break;
+		}
 		case IMAGE_DATA_TYPE_HALF4:
-			pixels[0] = TEX_IMAGE_MISSING_R;
-			pixels[1] = TEX_IMAGE_MISSING_G;
-			pixels[2] = TEX_IMAGE_MISSING_B;
-			pixels[3] = TEX_IMAGE_MISSING_A;
+		{
+			half4 *pixels = (half4*)device_pixels;
+			pixels[0].x = TEX_IMAGE_MISSING_R;
+			pixels[0].y = TEX_IMAGE_MISSING_G;
+			pixels[0].z = TEX_IMAGE_MISSING_B;
+			pixels[0].w = TEX_IMAGE_MISSING_A;
 			break;
+		}
 		case IMAGE_DATA_TYPE_HALF:
+		{
+			half *pixels = (half*)device_pixels;
 			pixels[0] = TEX_IMAGE_MISSING_R;
 			break;
+		}
 		default:
 			assert(0);
 	}
@@ -615,9 +659,115 @@ void ImageManager::file_load_failed(Image *img,
 	img->mem = tex_img;
 	img->mem->interpolation = img->interpolation;
 	img->mem->extension = img->extension;
+	img->mem->grid_type = IMAGE_GRID_TYPE_DEFAULT;
+
 	tex_img->copy_to_device();
 }
 
+#ifdef WITH_OPENVDB
+template<typename DeviceType>
+void ImageManager::file_load_extern_vdb(Device *device,
+                                        Image *img,
+                                        ImageDataType type)
+{
+	VLOG(1) << "Loading external VDB " << img->filename
+	        << ", Grid: " << img->grid_name;
+
+	device_vector<DeviceType> *tex_img =
+	        new device_vector<DeviceType>(device,
+	                                      img->mem_name.c_str(),
+	                                      MEM_TEXTURE);
+
+	/* Retrieve metadata. */
+	int width, height, depth, components;
+	if(!file_load_image_generic(img, NULL, width, height, depth, components)) {
+		file_load_failed<DeviceType>(img, type, tex_img);
+		return;
+	}
+
+	if(device->info.type == DEVICE_CPU && 0) {
+		/* Load pointer to OpenVDB grid into texture. */
+		device_memory *tex_vdb = NULL;
+		{
+			thread_scoped_lock device_lock(device_mutex);
+			tex_vdb = openvdb_load_device_extern(device,
+												 img->filename,
+												 img->grid_name,
+												 img->mem_name,
+												 img->interpolation,
+												 img->extension,
+												 components > 1);
+
+		}
+
+		if(tex_vdb) {
+			img->mem = tex_vdb;
+			delete tex_img;
+			tex_img = NULL;
+		}
+		else {
+			file_load_failed<DeviceType>(img, type, tex_img);
+		}
+	}
+	else {
+		/* Load VDB as device_vector. */
+		int sparse_size = -1;
+		vector<int> sparse_index;
+		openvdb_load_preprocess(img->filename, img->grid_name, components,
+		                        img->isovalue, &sparse_index, sparse_size);
+
+		/* Allocate space for image. */
+		float *pixels;
+		{
+			thread_scoped_lock device_lock(device_mutex);
+			if(sparse_size > -1) {
+				pixels = (float*)tex_img->alloc(sparse_size);
+			}
+			else {
+				pixels = (float*)tex_img->alloc(width, height, depth);
+			}
+		}
+
+		if(!pixels) {
+			/* Could be that we've run out of memory. */
+			file_load_failed<DeviceType>(img, type, tex_img);
+			return;
+		}
+
+		/* Load image. */
+		openvdb_load_image(img->filename, img->grid_name, components, pixels, &sparse_index);
+
+		/* Allocate space for sparse_index if it exists. */
+		if(sparse_size > -1) {
+			tex_img->grid_type = IMAGE_GRID_TYPE_SPARSE;
+
+			if(!allocate_sparse_index(device, (device_memory*)tex_img,
+			                          &sparse_index, img->mem_name))
+			{
+				/* Could be that we've run out of memory. */
+				file_load_failed<DeviceType>(img, type, tex_img);
+				return;
+			}
+		}
+		else {
+			tex_img->grid_type = IMAGE_GRID_TYPE_DEFAULT;
+		}
+
+		/* Set metadata and copy. */
+		tex_img->real_width = width;
+		tex_img->real_height = height;
+	

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list