[Bf-blender-cvs] [070a668d048] master: Code refactor: move more memory allocation logic into device API.
Brecht Van Lommel
noreply at git.blender.org
Tue Oct 24 02:12:09 CEST 2017
Commit: 070a668d04844610059aaedc80c49e9038fd1779
Author: Brecht Van Lommel
Date: Sat Oct 21 01:09:59 2017 +0200
Branches: master
https://developer.blender.org/rB070a668d04844610059aaedc80c49e9038fd1779
Code refactor: move more memory allocation logic into device API.
* Remove tex_* and pixels_* functions, replace by mem_*.
* Add MEM_TEXTURE and MEM_PIXELS as memory types recognized by devices.
* No longer create device_memory and call mem_* directly, always go
through device_only_memory, device_vector and device_pixels.
===================================================================
M intern/cycles/device/device.cpp
M intern/cycles/device/device.h
M intern/cycles/device/device_cpu.cpp
M intern/cycles/device/device_cuda.cpp
M intern/cycles/device/device_denoising.cpp
M intern/cycles/device/device_memory.cpp
M intern/cycles/device/device_memory.h
M intern/cycles/device/device_multi.cpp
M intern/cycles/device/device_network.cpp
M intern/cycles/device/device_network.h
M intern/cycles/device/device_split_kernel.cpp
M intern/cycles/device/opencl/memory_manager.cpp
M intern/cycles/device/opencl/opencl_base.cpp
M intern/cycles/device/opencl/opencl_split.cpp
M intern/cycles/render/bake.cpp
M intern/cycles/render/buffers.cpp
M intern/cycles/render/buffers.h
M intern/cycles/render/image.cpp
M intern/cycles/render/integrator.cpp
M intern/cycles/render/light.cpp
M intern/cycles/render/mesh.cpp
M intern/cycles/render/mesh_displace.cpp
M intern/cycles/render/object.cpp
M intern/cycles/render/particles.cpp
M intern/cycles/render/scene.cpp
M intern/cycles/render/session.cpp
M intern/cycles/render/shader.cpp
M intern/cycles/render/svm.cpp
M intern/cycles/render/tables.cpp
M intern/cycles/util/util_vector.h
===================================================================
diff --git a/intern/cycles/device/device.cpp b/intern/cycles/device/device.cpp
index 9de10c184fb..41fbe7ce81b 100644
--- a/intern/cycles/device/device.cpp
+++ b/intern/cycles/device/device.cpp
@@ -85,28 +85,12 @@ Device::~Device()
}
}
-void Device::pixels_alloc(device_memory& mem)
-{
- mem_alloc(mem);
-}
-
-void Device::pixels_copy_from(device_memory& mem, int y, int w, int h)
-{
- if(mem.data_type == TYPE_HALF)
- mem_copy_from(mem, y, w, h, sizeof(half4));
- else
- mem_copy_from(mem, y, w, h, sizeof(uchar4));
-}
-
-void Device::pixels_free(device_memory& mem)
-{
- mem_free(mem);
-}
-
void Device::draw_pixels(device_memory& rgba, int y, int w, int h, int dx, int dy, int width, int height, bool transparent,
const DeviceDrawParams &draw_params)
{
- pixels_copy_from(rgba, y, w, h);
+ assert(mem.type == MEM_PIXELS);
+
+ mem_copy_from(rgba, y, w, h, rgba.memory_elements_size(1));
if(transparent) {
glEnable(GL_BLEND);
diff --git a/intern/cycles/device/device.h b/intern/cycles/device/device.h
index 6bb65cde2a3..316bf70a5c3 100644
--- a/intern/cycles/device/device.h
+++ b/intern/cycles/device/device.h
@@ -281,28 +281,12 @@ public:
/* statistics */
Stats &stats;
- /* regular memory */
- virtual void mem_alloc(device_memory& mem) = 0;
- virtual void mem_copy_to(device_memory& mem) = 0;
- virtual void mem_copy_from(device_memory& mem,
- int y, int w, int h, int elem) = 0;
- virtual void mem_zero(device_memory& mem) = 0;
- virtual void mem_free(device_memory& mem) = 0;
-
+ /* memory alignment */
virtual int mem_address_alignment() { return 16; }
/* constant memory */
virtual void const_copy_to(const char *name, void *host, size_t size) = 0;
- /* texture memory */
- virtual void tex_alloc(device_memory& /*mem*/) {};
- virtual void tex_free(device_memory& /*mem*/) {};
-
- /* pixel memory */
- virtual void pixels_alloc(device_memory& mem);
- virtual void pixels_copy_from(device_memory& mem, int y, int w, int h);
- virtual void pixels_free(device_memory& mem);
-
/* open shading language, only for CPU device */
virtual void *osl_memory() { return NULL; }
@@ -349,6 +333,20 @@ public:
static void tag_update();
static void free_memory();
+
+protected:
+ /* Memory allocation, only accessed through device_memory. */
+ friend class MultiDevice;
+ friend class DeviceServer;
+ friend class device_memory;
+
+ virtual void mem_alloc(device_memory& mem) = 0;
+ virtual void mem_copy_to(device_memory& mem) = 0;
+ virtual void mem_copy_from(device_memory& mem,
+ int y, int w, int h, int elem) = 0;
+ virtual void mem_zero(device_memory& mem) = 0;
+ virtual void mem_free(device_memory& mem) = 0;
+
private:
/* Indicted whether device types and devices lists were initialized. */
static bool need_types_update, need_devices_update;
diff --git a/intern/cycles/device/device_cpu.cpp b/intern/cycles/device/device_cpu.cpp
index b4398f21014..32ab18fe164 100644
--- a/intern/cycles/device/device_cpu.cpp
+++ b/intern/cycles/device/device_cpu.cpp
@@ -209,7 +209,7 @@ public:
CPUDevice(DeviceInfo& info_, Stats &stats_, bool background_)
: Device(info_, stats_, background_),
- texture_info(this, "__texture_info"),
+ texture_info(this, "__texture_info", MEM_TEXTURE),
#define REGISTER_KERNEL(name) name ## _kernel(KERNEL_FUNCTIONS(name))
REGISTER_KERNEL(path_trace),
REGISTER_KERNEL(convert_to_half_float),
@@ -269,7 +269,7 @@ public:
~CPUDevice()
{
task_pool.stop();
- tex_free(texture_info);
+ texture_info.free();
}
virtual bool show_samples() const
@@ -280,33 +280,50 @@ public:
void load_texture_info()
{
if(need_texture_info) {
- tex_free(texture_info);
- tex_alloc(texture_info);
+ texture_info.copy_to_device();
need_texture_info = false;
}
}
void mem_alloc(device_memory& mem)
{
- if(mem.name) {
- VLOG(1) << "Buffer allocate: " << mem.name << ", "
- << string_human_readable_number(mem.memory_size()) << " bytes. ("
- << string_human_readable_size(mem.memory_size()) << ")";
+ if(mem.type == MEM_TEXTURE) {
+ assert(!"mem_alloc not supported for textures.");
}
+ else {
+ if(mem.name) {
+ VLOG(1) << "Buffer allocate: " << mem.name << ", "
+ << string_human_readable_number(mem.memory_size()) << " bytes. ("
+ << string_human_readable_size(mem.memory_size()) << ")";
+ }
- mem.device_pointer = mem.data_pointer;
+ mem.device_pointer = mem.data_pointer;
- if(!mem.device_pointer) {
- mem.device_pointer = (device_ptr)malloc(mem.memory_size());
- }
+ if(!mem.device_pointer) {
+ mem.device_pointer = (device_ptr)malloc(mem.memory_size());
+ }
- mem.device_size = mem.memory_size();
- stats.mem_alloc(mem.device_size);
+ mem.device_size = mem.memory_size();
+ stats.mem_alloc(mem.device_size);
+ }
}
- void mem_copy_to(device_memory& /*mem*/)
+ void mem_copy_to(device_memory& mem)
{
- /* no-op */
+ if(mem.type == MEM_TEXTURE) {
+ tex_free(mem);
+ tex_alloc(mem);
+ }
+ else if(mem.type == MEM_PIXELS) {
+ assert(!"mem_copy_to not supported for pixels.");
+ }
+ else {
+ if(!mem.device_pointer) {
+ mem_alloc(mem);
+ }
+
+ /* copy is no-op */
+ }
}
void mem_copy_from(device_memory& /*mem*/,
@@ -318,12 +335,21 @@ public:
void mem_zero(device_memory& mem)
{
- memset((void*)mem.device_pointer, 0, mem.memory_size());
+ if(!mem.device_pointer) {
+ mem_alloc(mem);
+ }
+
+ if(mem.device_pointer) {
+ memset((void*)mem.device_pointer, 0, mem.memory_size());
+ }
}
void mem_free(device_memory& mem)
{
- if(mem.device_pointer) {
+ if(mem.type == MEM_TEXTURE) {
+ tex_free(mem);
+ }
+ else if(mem.device_pointer) {
if(!mem.data_pointer) {
free((void*)mem.device_pointer);
}
@@ -354,7 +380,7 @@ public:
kernel_tex_copy(&kernel_globals,
mem.name,
mem.data_pointer,
- mem.data_width);
+ mem.data_size);
}
else {
/* Image Texture. */
@@ -431,13 +457,13 @@ public:
bool denoising_set_tiles(device_ptr *buffers, DenoisingTask *task)
{
- mem_alloc(task->tiles_mem);
-
TilesInfo *tiles = (TilesInfo*) task->tiles_mem.data_pointer;
for(int i = 0; i < 9; i++) {
tiles->buffers[i] = buffers[i];
}
+ task->tiles_mem.copy_to_device();
+
return true;
}
@@ -723,8 +749,7 @@ public:
/* allocate buffer for kernel globals */
device_only_memory<KernelGlobals> kgbuffer(this, "kernel_globals");
- kgbuffer.resize(1);
- mem_alloc(kgbuffer);
+ kgbuffer.alloc_to_device(1);
KernelGlobals *kg = new ((void*) kgbuffer.device_pointer) KernelGlobals(thread_kernel_globals_init());
@@ -734,8 +759,7 @@ public:
requested_features.max_closure = MAX_CLOSURE;
if(!split_kernel->load_kernels(requested_features)) {
thread_kernel_globals_free((KernelGlobals*)kgbuffer.device_pointer);
- mem_free(kgbuffer);
-
+ kgbuffer.free();
delete split_kernel;
return;
}
@@ -766,7 +790,7 @@ public:
thread_kernel_globals_free((KernelGlobals*)kgbuffer.device_pointer);
kg->~KernelGlobals();
- mem_free(kgbuffer);
+ kgbuffer.free();
delete split_kernel;
}
diff --git a/intern/cycles/device/device_cuda.cpp b/intern/cycles/device/device_cuda.cpp
index be606a92434..aa6386e455b 100644
--- a/intern/cycles/device/device_cuda.cpp
+++ b/intern/cycles/device/device_cuda.cpp
@@ -218,7 +218,7 @@ public:
CUDADevice(DeviceInfo& info, Stats &stats, bool background_)
: Device(info, stats, background_),
- texture_info(this, "__texture_info")
+ texture_info(this, "__texture_info", MEM_TEXTURE)
{
first_error = true;
background = background_;
@@ -275,7 +275,7 @@ public:
delete split_kernel;
if(info.has_bindless_textures) {
- tex_free(texture_info);
+ texture_info.free();
}
cuda_assert(cuCtxDestroy(cuContext));
@@ -548,20 +548,19 @@ public:
void load_texture_info()
{
if(info.has_bindless_textures && need_texture_info) {
- tex_free(texture_info);
- tex_alloc(texture_info);
+ texture_info.copy_to_device();
need_texture_info = false;
}
}
- void mem_alloc(device_memory& mem)
+ void generic_alloc(device_memory& mem)
{
CUDAContextScope scope(this);
if(mem.name) {
VLOG(1) << "Buffer allocate: " << mem.name << ", "
- << string_human_readable_number(mem.memory_size()) << " bytes. ("
- << string_human_readable_size(mem.memory_size()) << ")";
+ << string_human_readable_number(mem.memory_size()) << " bytes. ("
+ << string_human_readable_size(mem.memory_size()) << ")";
}
CUdeviceptr device_pointer;
@@ -572,31 +571,88 @@ public:
stats.mem_alloc(size);
}
+ void generic_copy_to(device_memory& mem)
+ {
+ if(mem.device_pointer) {
+ CUDAContextScope scope(this);
+ cuda_assert(cuMemcpyHtoD(cuda_device_ptr(mem.device_pointer), (void*)mem.data_pointer, mem.memory_size()));
+ }
+ }
+
+ void generic_free(device_memory& mem)
+ {
+ if(mem.device_pointer) {
+ CUDAContextScope scope(this);
+
+ cuda_assert(cuMemFree(cuda_device_ptr(mem.device_pointer)));
+
+ mem.device_pointer = 0;
+
+ stats.mem_free(mem.device_size);
+ mem.device_size = 0;
+ }
+ }
+
+ void mem_alloc(device_memory& mem)
+ {
+ if(mem.type == MEM_PIXELS && !background) {
+ pixels_alloc(mem);
+ }
+ else if(mem.type == MEM_TEXTURE) {
+ assert(!"mem_alloc not supported for textures.");
+ }
+ else {
+ generic_alloc(mem);
+ }
+ }
+
void mem_copy_to(device_memory& mem)
{
- CUDAContextScope scope(this);
+ if(mem.type == MEM_PIXELS) {
+ assert(!"mem_copy_to not supported for pixels.");
+ }
+ else if(mem.type == MEM_TEXTURE) {
+ tex_free(mem);
+ tex_alloc(mem);
+ }
+ else {
+ if(!mem.device_pointer) {
+ generic_alloc(mem);
+ }
- if(mem.device_pointer)
- cuda_assert(cuMemcpyHtoD(cuda_device_ptr(mem.device_pointer), (void*)mem.data_pointer, mem.memory_size()));
+ generic_copy_to(mem);
+ }
}
void mem_copy_from(device_memory& mem, int y, int w, int h, int elem)
{
- CUDAContextScope scope(this);
- size_t offset = elem*y*w;
- size_t size = elem*w*h;
-
- if(mem.device_pointer) {
- cuda_assert(cuMemcpyDtoH((uchar*)mem.data_pointer + offset,
- (CUdeviceptr)(mem.device_pointer + offset), size));
+
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list