Commit: f2809ae0a671057caa1005e2b9cc91648c33dd1f
Author: Sergey Sharybin
Date:   Tue Aug 8 17:17:04 2017 +0200
Branches: master
https://developer.blender.org/rBf2809ae0a671057caa1005e2b9cc91648c33dd1f

Cycles: Fix crash changing image after recent OpenCL changes

Steps to reproduce:
- Create shader Image texture -> Diffuse BSDF -> Output. Do NOT select image 
yet!
- Start viewport render.
- Select image from the ID browser of Image Texture node.

Thing is: with the memory manager we always need to inform device that memory
was freed.

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

M       intern/cycles/render/image.cpp
M       intern/cycles/render/image.h

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

diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index 80ec77f8b4a..cfdf26c6787 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -716,12 +716,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_float4_image[slot] == NULL)
                        dscene->tex_float4_image[slot] = new 
device_vector<float4>();
                device_vector<float4>& tex_img = 
*dscene->tex_float4_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::FLOAT, float>(img,
                                                            type,
                                                            texture_limit,
@@ -748,12 +743,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_float_image[slot] == NULL)
                        dscene->tex_float_image[slot] = new 
device_vector<float>();
                device_vector<float>& tex_img = *dscene->tex_float_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::FLOAT, float>(img,
                                                            type,
                                                            texture_limit,
@@ -777,12 +767,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_byte4_image[slot] == NULL)
                        dscene->tex_byte4_image[slot] = new 
device_vector<uchar4>();
                device_vector<uchar4>& tex_img = *dscene->tex_byte4_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::UINT8, uchar>(img,
                                                            type,
                                                            texture_limit,
@@ -809,12 +794,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_byte_image[slot] == NULL)
                        dscene->tex_byte_image[slot] = new 
device_vector<uchar>();
                device_vector<uchar>& tex_img = *dscene->tex_byte_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::UINT8, uchar>(img,
                                                            type,
                                                            texture_limit,
@@ -837,12 +817,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_half4_image[slot] == NULL)
                        dscene->tex_half4_image[slot] = new 
device_vector<half4>();
                device_vector<half4>& tex_img = *dscene->tex_half4_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::HALF, half>(img,
                                                          type,
                                                          texture_limit,
@@ -868,12 +843,7 @@ void ImageManager::device_load_image(Device *device,
                if(dscene->tex_half_image[slot] == NULL)
                        dscene->tex_half_image[slot] = new 
device_vector<half>();
                device_vector<half>& tex_img = *dscene->tex_half_image[slot];
-
-               if(tex_img.device_pointer) {
-                       thread_scoped_lock device_lock(device_mutex);
-                       device->tex_free(tex_img);
-               }
-
+               device_tex_free_safe(device, tex_img);
                if(!file_load_image<TypeDesc::HALF, half>(img,
                                                          type,
                                                          texture_limit,
@@ -957,11 +927,7 @@ void ImageManager::device_free_image(Device *device, 
DeviceScene *dscene, ImageD
                                        tex_img = NULL;
                        }
                        if(tex_img) {
-                               if(tex_img->device_pointer) {
-                                       thread_scoped_lock 
device_lock(device_mutex);
-                                       device->tex_free(*tex_img);
-                               }
-
+                               device_tex_free_safe(device, *tex_img);
                                delete tex_img;
                        }
                }
@@ -1097,5 +1063,16 @@ void ImageManager::device_free(Device *device, 
DeviceScene *dscene)
        dscene->tex_half_image.clear();
 }
 
+void ImageManager::device_tex_free_safe(Device *device, device_memory& mem)
+{
+       if(mem.device_pointer) {
+               thread_scoped_lock device_lock(device_mutex);
+               device->tex_free(mem);
+       }
+       else {
+               device->tex_free(mem);
+       }
+}
+
 CCL_NAMESPACE_END
 
diff --git a/intern/cycles/render/image.h b/intern/cycles/render/image.h
index c86d1cbedbf..abd1abbd729 100644
--- a/intern/cycles/render/image.h
+++ b/intern/cycles/render/image.h
@@ -160,6 +160,11 @@ private:
                               DeviceScene *dscene,
                               ImageDataType type,
                               int slot);
+
+       /* Will do locking when needed and make sure possible memory manager 
from
+        * the device implementation is aware of freed texture.
+        */
+       void device_tex_free_safe(Device *device, device_memory& mem);
 };
 
 CCL_NAMESPACE_END

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to