[Bf-blender-cvs] [5f3b3554772] soc-2018-volumes: sparse volumes initial commit

Geraldine Chua noreply at git.blender.org
Mon May 28 20:32:13 CEST 2018


Commit: 5f3b35547725159bb33e383a81dc93dc92f4ad4f
Author: Geraldine Chua
Date:   Tue May 29 01:33:47 2018 +0800
Branches: soc-2018-volumes
https://developer.blender.org/rB5f3b35547725159bb33e383a81dc93dc92f4ad4f

sparse volumes initial commit

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

M	intern/cycles/device/device_cpu.cpp
M	intern/cycles/device/device_memory.h
M	intern/cycles/device/opencl/opencl.h
M	intern/cycles/render/image.cpp
M	intern/cycles/render/image.h
M	intern/cycles/render/mesh_volume.cpp
M	intern/cycles/util/CMakeLists.txt
M	intern/cycles/util/util_texture.h
A	intern/cycles/util/util_volume.cpp
A	intern/cycles/util/util_volume.h

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

diff --git a/intern/cycles/device/device_cpu.cpp b/intern/cycles/device/device_cpu.cpp
index 6be60f8bbb6..e9898cba20d 100644
--- a/intern/cycles/device/device_cpu.cpp
+++ b/intern/cycles/device/device_cpu.cpp
@@ -52,6 +52,8 @@
 #include "util/util_progress.h"
 #include "util/util_system.h"
 #include "util/util_thread.h"
+/* gchua: fix later */
+#include "util/util_volume.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -375,9 +377,14 @@ public:
 
 	void tex_alloc(device_memory& mem)
 	{
+		size_t total_memory = mem.memory_size();
+		if(mem.helper != NULL) {
+			total_memory += mem.helper->memory_size();
+		}
+
 		VLOG(1) << "Texture allocate: " << mem.name << ", "
-		        << string_human_readable_number(mem.memory_size()) << " bytes. ("
-		        << string_human_readable_size(mem.memory_size()) << ")";
+		        << string_human_readable_number(total_memory) << " bytes. ("
+		        << string_human_readable_size(total_memory) << ")";
 
 		if(mem.interpolation == INTERPOLATION_NONE) {
 			/* Data texture. */
@@ -408,16 +415,38 @@ public:
 			info.cl_buffer = 0;
 			info.interpolation = mem.interpolation;
 			info.extension = mem.extension;
-			info.width = mem.data_width;
-			info.height = mem.data_height;
-			info.depth = mem.data_depth;
-
+			if(mem.helper != NULL) {
+				/* If mem is a sparse volume, its real (tile) dimensions
+				 * are stored in the helper texture. */
+				info.width = mem.helper->data_width * CUBE_SIZE;
+				info.height = mem.helper->data_height * CUBE_SIZE;
+				info.depth = mem.helper->data_depth * CUBE_SIZE;
+				info.indexes = (uint64_t)mem.helper->host_pointer;
+			}
+			else {
+				info.width = mem.data_width;
+				info.height = mem.data_height;
+				info.depth = mem.data_depth;
+				/* todo (gchua): what if the valid host pointer is 0 */
+				info.indexes = (uint64_t)0;
+			}
+			VLOG(1) << "Texture width: " << mem.data_width << ", " << info.width;
+			VLOG(1) << "Texture height: " << mem.data_height << ", " << info.height;
+			VLOG(1) << "Texture depth: " << mem.data_depth << ", " << info.depth;
+			VLOG(1) << "Texture helper: " << info.indexes;
 			need_texture_info = true;
 		}
 
 		mem.device_pointer = (device_ptr)mem.host_pointer;
 		mem.device_size = mem.memory_size();
 		stats.mem_alloc(mem.device_size);
+
+		if(mem.helper != NULL) {
+			mem.helper->device_pointer = (device_ptr)mem.helper->host_pointer;
+			mem.helper->device_size = mem.helper->memory_size();
+			stats.mem_alloc(mem.helper->device_size);
+		}
+
 	}
 
 	void tex_free(device_memory& mem)
diff --git a/intern/cycles/device/device_memory.h b/intern/cycles/device/device_memory.h
index d8fe41e78bb..cfe95b5b0c5 100644
--- a/intern/cycles/device/device_memory.h
+++ b/intern/cycles/device/device_memory.h
@@ -197,6 +197,7 @@ public:
 	device_ptr device_pointer;
 	void *host_pointer;
 	void *shared_pointer;
+	device_memory *helper;
 
 	virtual ~device_memory();
 
diff --git a/intern/cycles/device/opencl/opencl.h b/intern/cycles/device/opencl/opencl.h
index 85ef14ee29a..0d0f838bc69 100644
--- a/intern/cycles/device/opencl/opencl.h
+++ b/intern/cycles/device/opencl/opencl.h
@@ -549,7 +549,7 @@ private:
 	MemoryManager memory_manager;
 	friend class MemoryManager;
 
-	static_assert_align(TextureInfo, 16);
+	static_assert_align(TextureInfo, 20);
 	device_vector<TextureInfo> texture_info;
 
 	typedef map<string, device_memory*> TexturesMap;
diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index 9c5e32e8219..c37d7e02d43 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -22,7 +22,10 @@
 #include "util/util_logging.h"
 #include "util/util_path.h"
 #include "util/util_progress.h"
+#include "util/util_string.h"
 #include "util/util_texture.h"
+/* gchua: fix later */
+#include "util/util_volume.h"
 
 #ifdef WITH_OSL
 #include <OSL/oslexec.h>
@@ -192,6 +195,8 @@ bool ImageManager::get_image_metadata(const string& filename,
 	else {
 		metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
 	}
+	VLOG(1) << filename;
+	VLOG(1) << metadata.type;
 
 	in->close();
 	delete in;
@@ -235,6 +240,8 @@ string ImageManager::name_from_type(int type)
 		return "half4";
 	else if(type == IMAGE_DATA_TYPE_HALF)
 		return "half";
+	else if(type == IMAGE_DATA_TYPE_VOLUME)
+		return "volume";
 	else
 		return "byte4";
 }
@@ -574,6 +581,7 @@ bool ImageManager::file_load_image(Image *img,
 	 * but device doesn't support single channel textures.
 	 */
 	bool is_rgba = (type == IMAGE_DATA_TYPE_FLOAT4 ||
+	                type == IMAGE_DATA_TYPE_VOLUME ||
 	                type == IMAGE_DATA_TYPE_HALF4 ||
 	                type == IMAGE_DATA_TYPE_BYTE4);
 	if(is_rgba) {
@@ -682,6 +690,71 @@ bool ImageManager::file_load_image(Image *img,
 	return true;
 }
 
+
+/* If volume, call this to convert to sparse grid. */
+template<TypeDesc::BASETYPE FileFormat,
+         typename StorageType,
+         typename DeviceType>
+bool ImageManager::file_load_image(Image *img,
+                                   ImageDataType type,
+                                   int texture_limit,
+                                   device_vector<VolumeTile>& tex_img,
+                                   device_vector<int>& tex_helper)
+{
+
+	device_vector<DeviceType> *tex_img_raw
+		= new device_vector<DeviceType>(NULL, img->mem_name.c_str(), MEM_TEXTURE);
+
+	if(!file_load_image<FileFormat, StorageType, DeviceType>(img, type, texture_limit, *tex_img_raw)) {
+		 return false;
+	}
+
+	VLOG(1) << "Memory usage of raw volume texture: "
+		    << string_human_readable_size(tex_img_raw->memory_size());
+
+	DeviceType *data = tex_img_raw->data();
+	size_t tile_width = compute_tile_resolution(tex_img_raw->data_width);
+	size_t tile_height = compute_tile_resolution(tex_img_raw->data_height);
+	size_t tile_depth = compute_tile_resolution(tex_img_raw->data_depth);
+
+	vector<VolumeTile> sparse_grid;
+	vector<int> indexes;
+	create_sparse_grid(data,
+	                   tex_img_raw->data_width,
+	                   tex_img_raw->data_height,
+	                   tex_img_raw->data_depth,
+	                   sparse_grid,
+	                   indexes);
+
+	VolumeTile *texture_pixels;
+	int *texture_indexes;
+	{
+		/* Since only active tiles are stored in tex_img, its
+		 * allocated memory will be less than the actual resolution
+		 * of the volume. We store the true resolution (in tiles) in the
+		 * tex_helper instead, since it needs to be allocated enough
+		 * space to track all tiles anyway. */
+		thread_scoped_lock device_lock(device_mutex);
+		texture_pixels = (VolumeTile*)tex_img.alloc(sparse_grid.size());
+		texture_indexes = (int*)tex_helper.alloc(tile_width,
+												 tile_height,
+												 tile_depth);
+	}
+
+	memcpy(texture_pixels,
+		   &sparse_grid[0],
+		   sparse_grid.size() * sizeof(VolumeTile));
+
+	memcpy(texture_indexes,
+		   &indexes[0],
+		   indexes.size() * sizeof(int));
+
+	VLOG(1) << "Memory usage of sparse grid: "
+	        << string_human_readable_size(tex_img.memory_size());
+
+	return true;
+}
+
 void ImageManager::device_load_image(Device *device,
                                      Scene *scene,
                                      ImageDataType type,
@@ -705,9 +778,13 @@ void ImageManager::device_load_image(Device *device,
 	int flat_slot = type_index_to_flattened_slot(slot, type);
 	img->mem_name = string_printf("__tex_image_%s_%03d", name_from_type(type).c_str(), flat_slot);
 
-	/* Free previous texture in slot. */
+	/* Free previous texture(s) in slot. */
 	if(img->mem) {
 		thread_scoped_lock device_lock(device_mutex);
+		if(img->mem->helper) {
+			delete img->mem->helper;
+			img->mem->helper = NULL;
+		}
 		delete img->mem;
 		img->mem = NULL;
 	}
@@ -857,6 +934,29 @@ void ImageManager::device_load_image(Device *device,
 		thread_scoped_lock device_lock(device_mutex);
 		tex_img->copy_to_device();
 	}
+	else if(type == IMAGE_DATA_TYPE_VOLUME) {
+		device_vector<VolumeTile> *tex_img
+			= new device_vector<VolumeTile>(device, img->mem_name.c_str(), MEM_TEXTURE);
+		device_vector<int> *tex_helper
+			= new device_vector<int>(device, (img->mem_name + "_helper").c_str(), MEM_TEXTURE);
+
+		if(!file_load_image<TypeDesc::FLOAT, float, float4>(img,
+															type,
+															texture_limit,
+															*tex_img,
+															*tex_helper)) {
+			 /* todo (gchua): fix this later */
+		}
+
+		tex_img->helper = tex_helper;
+		img->mem = tex_img;
+		img->mem->interpolation = img->interpolation;
+		img->mem->extension = img->extension;
+
+		thread_scoped_lock device_lock(device_mutex);
+		tex_img->copy_to_device();
+		tex_helper->copy_to_device();
+	}
 
 	img->need_load = false;
 }
diff --git a/intern/cycles/render/image.h b/intern/cycles/render/image.h
index 5391490d993..dae911d38bb 100644
--- a/intern/cycles/render/image.h
+++ b/intern/cycles/render/image.h
@@ -24,6 +24,8 @@
 #include "util/util_string.h"
 #include "util/util_thread.h"
 #include "util/util_vector.h"
+/* gchua: fixxxxx */
+#include "util/util_volume.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -151,6 +153,15 @@ private:
 	                     int texture_limit,
 	                     device_vector<DeviceType>& tex_img);
 
+	template<TypeDesc::BASETYPE FileFormat,
+	         typename StorageType,
+	         typename DeviceType>
+	bool file_load_image(Image *img,
+	                     ImageDataType type,
+	                     int texture_limit,
+	                     device_vector<VolumeTile>& tex_img,
+	                     device_vector<int>& tex_helper);
+
 	int max_flattened_slot(ImageDataType type);
 	int type_index_to_flattened_slot(int slot, ImageDataType type);
 	int flattened_slot_to_type_index(int flat_slot, ImageDataType *type);
diff --git a/intern/cycles/render/mesh_volume.cpp b/intern/cycles/render/mesh_volume.cpp
index d1c49b456ff..4c0a7a80b6f 100644
--- a/intern/cycles/render/mesh_volume.cpp
+++ b/intern/cycles/render/mesh_volume.cpp
@@ -22,6 +22,7 @@
 #include "util/util_logging.h"
 #include "util/util_progress.h"
 #include "util/util_types.h"
+#include "util/util_volume.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -113,8 +114,6 @@ struct VolumeParams {
 	int pad_size;
 };
 
-static const int CUBE_SIZE = 8;
-
 /* Create a mesh from a volum

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list