Code refactor: move more memory allocation logic into device API.
[blender.git] / intern / cycles / render / image.cpp
index e7f5ff002b7bb80df08457d6ca414b84fe9351f3..625901ff258ea33acb72f0e01ff50a9f8fc2d8a4 100644 (file)
@@ -532,7 +532,8 @@ bool ImageManager::file_load_image(Image *img,
                pixels = &pixels_storage[0];
        }
        else {
-               pixels = (StorageType*)tex_img.resize(width, height, depth);
+               thread_scoped_lock device_lock(device_mutex);
+               pixels = (StorageType*)tex_img.alloc(width, height, depth);
        }
        if(pixels == NULL) {
                /* Could be that we've run out of memory. */
@@ -686,9 +687,16 @@ bool ImageManager::file_load_image(Image *img,
                                         scale_factor,
                                         &scaled_pixels,
                                         &scaled_width, &scaled_height, &scaled_depth);
-               StorageType *texture_pixels = (StorageType*)tex_img.resize(scaled_width,
-                                                                          scaled_height,
-                                                                          scaled_depth);
+
+               StorageType *texture_pixels;
+
+               {
+                       thread_scoped_lock device_lock(device_mutex);
+                       texture_pixels = (StorageType*)tex_img.alloc(scaled_width,
+                                                                    scaled_height,
+                                                                    scaled_depth);
+               }
+
                memcpy(texture_pixels,
                       &scaled_pixels[0],
                       scaled_pixels.size() * sizeof(StorageType));
@@ -722,14 +730,14 @@ void ImageManager::device_load_image(Device *device,
        /* Free previous texture in slot. */
        if(img->mem) {
                thread_scoped_lock device_lock(device_mutex);
-               device->tex_free(*img->mem);
                delete img->mem;
                img->mem = NULL;
        }
 
        /* Create new texture. */
        if(type == IMAGE_DATA_TYPE_FLOAT4) {
-               device_vector<float4> *tex_img = new device_vector<float4>(device, name.c_str());
+               device_vector<float4> *tex_img
+                       = new device_vector<float4>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::FLOAT, float>(img,
                                                            type,
@@ -737,7 +745,7 @@ void ImageManager::device_load_image(Device *device,
                                                            *tex_img))
                {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       float *pixels = (float*)tex_img->resize(1, 1);
+                       float *pixels = (float*)tex_img->alloc(1, 1);
 
                        pixels[0] = TEX_IMAGE_MISSING_R;
                        pixels[1] = TEX_IMAGE_MISSING_G;
@@ -746,9 +754,15 @@ void ImageManager::device_load_image(Device *device,
                }
 
                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();
        }
        else if(type == IMAGE_DATA_TYPE_FLOAT) {
-               device_vector<float> *tex_img = new device_vector<float>(device, name.c_str());
+               device_vector<float> *tex_img
+                       = new device_vector<float>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::FLOAT, float>(img,
                                                            type,
@@ -756,15 +770,21 @@ void ImageManager::device_load_image(Device *device,
                                                            *tex_img))
                {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       float *pixels = (float*)tex_img->resize(1, 1);
+                       float *pixels = (float*)tex_img->alloc(1, 1);
 
                        pixels[0] = TEX_IMAGE_MISSING_R;
                }
 
                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();
        }
        else if(type == IMAGE_DATA_TYPE_BYTE4) {
-               device_vector<uchar4> *tex_img = new device_vector<uchar4>(device, name.c_str());
+               device_vector<uchar4> *tex_img
+                       = new device_vector<uchar4>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::UINT8, uchar>(img,
                                                            type,
@@ -772,7 +792,7 @@ void ImageManager::device_load_image(Device *device,
                                                            *tex_img))
                {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       uchar *pixels = (uchar*)tex_img->resize(1, 1);
+                       uchar *pixels = (uchar*)tex_img->alloc(1, 1);
 
                        pixels[0] = (TEX_IMAGE_MISSING_R * 255);
                        pixels[1] = (TEX_IMAGE_MISSING_G * 255);
@@ -781,31 +801,43 @@ void ImageManager::device_load_image(Device *device,
                }
 
                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();
        }
        else if(type == IMAGE_DATA_TYPE_BYTE) {
-               device_vector<uchar> *tex_img = new device_vector<uchar>(device, name.c_str());
+               device_vector<uchar> *tex_img
+                       = new device_vector<uchar>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::UINT8, uchar>(img,
                                                            type,
                                                            texture_limit,
                                                            *tex_img)) {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       uchar *pixels = (uchar*)tex_img->resize(1, 1);
+                       uchar *pixels = (uchar*)tex_img->alloc(1, 1);
 
                        pixels[0] = (TEX_IMAGE_MISSING_R * 255);
                }
 
                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();
        }
        else if(type == IMAGE_DATA_TYPE_HALF4) {
-               device_vector<half4> *tex_img = new device_vector<half4>(device, name.c_str());
+               device_vector<half4> *tex_img
+                       = new device_vector<half4>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::HALF, half>(img,
                                                          type,
                                                          texture_limit,
                                                          *tex_img)) {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       half *pixels = (half*)tex_img->resize(1, 1);
+                       half *pixels = (half*)tex_img->alloc(1, 1);
 
                        pixels[0] = TEX_IMAGE_MISSING_R;
                        pixels[1] = TEX_IMAGE_MISSING_G;
@@ -814,37 +846,38 @@ void ImageManager::device_load_image(Device *device,
                }
 
                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();
        }
        else if(type == IMAGE_DATA_TYPE_HALF) {
-               device_vector<half> *tex_img = new device_vector<half>(device, name.c_str());
+               device_vector<half> *tex_img
+                       = new device_vector<half>(device, name.c_str(), MEM_TEXTURE);
 
                if(!file_load_image<TypeDesc::HALF, half>(img,
                                                          type,
                                                          texture_limit,
                                                          *tex_img)) {
                        /* on failure to load, we set a 1x1 pixels pink image */
-                       half *pixels = (half*)tex_img->resize(1, 1);
+                       half *pixels = (half*)tex_img->alloc(1, 1);
 
                        pixels[0] = TEX_IMAGE_MISSING_R;
                }
 
                img->mem = tex_img;
-       }
-
-       /* Copy to device. */
-       if(img->mem) {
                img->mem->interpolation = img->interpolation;
                img->mem->extension = img->extension;
 
                thread_scoped_lock device_lock(device_mutex);
-               device->tex_alloc(*img->mem);
+               tex_img->copy_to_device();
        }
 
-
        img->need_load = false;
 }
 
-void ImageManager::device_free_image(Device *device, ImageDataType type, int slot)
+void ImageManager::device_free_image(Device *, ImageDataType type, int slot)
 {
        Image *img = images[type][slot];
 
@@ -858,7 +891,6 @@ void ImageManager::device_free_image(Device *device, ImageDataType type, int slo
 
                if(img->mem) {
                        thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(*img->mem);
                        delete img->mem;
                }