Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / render / image.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "device/device.h"
18 #include "render/image.h"
19 #include "render/scene.h"
20 #include "render/stats.h"
21
22 #include "util/util_foreach.h"
23 #include "util/util_logging.h"
24 #include "util/util_path.h"
25 #include "util/util_progress.h"
26 #include "util/util_texture.h"
27 #include "util/util_unique_ptr.h"
28
29 #ifdef WITH_OSL
30 #include <OSL/oslexec.h>
31 #endif
32
33 CCL_NAMESPACE_BEGIN
34
35 namespace {
36
37 /* Some helpers to silence warning in templated function. */
38 bool isfinite(uchar /*value*/)
39 {
40         return true;
41 }
42 bool isfinite(half /*value*/)
43 {
44         return true;
45 }
46 bool isfinite(uint16_t  /*value*/)
47 {
48         return true;
49 }
50
51 /* The lower three bits of a device texture slot number indicate its type.
52  * These functions convert the slot ids from ImageManager "images" ones
53  * to device ones and vice verse.
54  */
55 int type_index_to_flattened_slot(int slot, ImageDataType type)
56 {
57         return (slot << IMAGE_DATA_TYPE_SHIFT) | (type);
58 }
59
60 int flattened_slot_to_type_index(int flat_slot, ImageDataType *type)
61 {
62         *type = (ImageDataType)(flat_slot & IMAGE_DATA_TYPE_MASK);
63         return flat_slot >> IMAGE_DATA_TYPE_SHIFT;
64 }
65
66 const char* name_from_type(ImageDataType type)
67 {
68         switch(type) {
69                 case IMAGE_DATA_TYPE_FLOAT4: return "float4";
70                 case IMAGE_DATA_TYPE_BYTE4: return "byte4";
71                 case IMAGE_DATA_TYPE_HALF4: return "half4";
72                 case IMAGE_DATA_TYPE_FLOAT: return "float";
73                 case IMAGE_DATA_TYPE_BYTE: return "byte";
74                 case IMAGE_DATA_TYPE_HALF: return "half";
75                 case IMAGE_DATA_TYPE_USHORT4: return "ushort4";
76                 case IMAGE_DATA_TYPE_USHORT: return "ushort";
77                 case IMAGE_DATA_NUM_TYPES:
78                         assert(!"System enumerator type, should never be used");
79                         return "";
80         }
81         assert(!"Unhandled image data type");
82         return "";
83 }
84
85 }  // namespace
86
87 ImageManager::ImageManager(const DeviceInfo& info)
88 {
89         need_update = true;
90         osl_texture_system = NULL;
91         animation_frame = 0;
92
93         /* Set image limits */
94         max_num_images = TEX_NUM_MAX;
95         has_half_images = info.has_half_images;
96
97         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
98                 tex_num_images[type] = 0;
99         }
100 }
101
102 ImageManager::~ImageManager()
103 {
104         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
105                 for(size_t slot = 0; slot < images[type].size(); slot++)
106                         assert(!images[type][slot]);
107         }
108 }
109
110 void ImageManager::set_osl_texture_system(void *texture_system)
111 {
112         osl_texture_system = texture_system;
113 }
114
115 bool ImageManager::set_animation_frame_update(int frame)
116 {
117         if(frame != animation_frame) {
118                 animation_frame = frame;
119
120                 for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
121                         for(size_t slot = 0; slot < images[type].size(); slot++) {
122                                 if(images[type][slot] && images[type][slot]->animated)
123                                         return true;
124                         }
125                 }
126         }
127
128         return false;
129 }
130
131 device_memory *ImageManager::image_memory(int flat_slot)
132 {
133         ImageDataType type;
134         int slot = flattened_slot_to_type_index(flat_slot, &type);
135
136         Image *img = images[type][slot];
137
138         return img->mem;
139 }
140
141 bool ImageManager::get_image_metadata(int flat_slot,
142                                       ImageMetaData& metadata)
143 {
144         if(flat_slot == -1) {
145                 return false;
146         }
147
148         ImageDataType type;
149         int slot = flattened_slot_to_type_index(flat_slot, &type);
150
151         Image *img = images[type][slot];
152         if(img) {
153                 metadata = img->metadata;
154                 return true;
155         }
156
157         return false;
158 }
159
160 bool ImageManager::get_image_metadata(const string& filename,
161                                       void *builtin_data,
162                                       ImageMetaData& metadata)
163 {
164         memset(&metadata, 0, sizeof(metadata));
165
166         if(builtin_data) {
167                 if(builtin_image_info_cb) {
168                         builtin_image_info_cb(filename, builtin_data, metadata);
169                 }
170                 else {
171                         return false;
172                 }
173
174                 if(metadata.is_float) {
175                         metadata.is_linear = true;
176                         metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4
177                                                                 : IMAGE_DATA_TYPE_FLOAT;
178                 }
179                 else {
180                         metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4
181                                                                 : IMAGE_DATA_TYPE_BYTE;
182                 }
183
184                 return true;
185         }
186
187         /* Perform preliminary checks, with meaningful logging. */
188         if(!path_exists(filename)) {
189                 VLOG(1) << "File '" << filename << "' does not exist.";
190                 return false;
191         }
192         if(path_is_directory(filename)) {
193                 VLOG(1) << "File '" << filename
194                         << "' is a directory, can't use as image.";
195                 return false;
196         }
197
198         unique_ptr<ImageInput> in(ImageInput::create(filename));
199
200         if(!in) {
201                 return false;
202         }
203
204         ImageSpec spec;
205         if(!in->open(filename, spec)) {
206                 return false;
207         }
208
209         metadata.width = spec.width;
210         metadata.height = spec.height;
211         metadata.depth = spec.depth;
212
213
214         /* Check the main format, and channel formats. */
215         size_t channel_size = spec.format.basesize();
216
217         if(spec.format.is_floating_point()) {
218                 metadata.is_float = true;
219                 metadata.is_linear = true;
220         }
221
222         for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
223                 channel_size = max(channel_size, spec.channelformats[channel].basesize());
224                 if(spec.channelformats[channel].is_floating_point()) {
225                         metadata.is_float = true;
226                         metadata.is_linear = true;
227                 }
228         }
229
230         /* check if it's half float */
231         if(spec.format == TypeDesc::HALF) {
232                 metadata.is_half = true;
233         }
234
235         /* basic color space detection, not great but better than nothing
236          * before we do OpenColorIO integration */
237         if(metadata.is_float) {
238                 string colorspace = spec.get_string_attribute("oiio:ColorSpace");
239
240                 metadata.is_linear = !(colorspace == "sRGB" ||
241                                                            colorspace == "GammaCorrected" ||
242                                                            (colorspace == "" &&
243                                                                    (strcmp(in->format_name(), "png") == 0 ||
244                                                                         strcmp(in->format_name(), "tiff") == 0 ||
245                                                                         strcmp(in->format_name(), "dpx") == 0 ||
246                                                                         strcmp(in->format_name(), "jpeg2000") == 0)));
247         }
248         else {
249                 metadata.is_linear = false;
250         }
251
252         /* set type and channels */
253         metadata.channels = spec.nchannels;
254
255         if(metadata.is_half) {
256                 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_HALF4
257                                                         : IMAGE_DATA_TYPE_HALF;
258         }
259         else if(metadata.is_float) {
260                 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4
261                                                         : IMAGE_DATA_TYPE_FLOAT;
262         }
263         else if(spec.format == TypeDesc::USHORT) {
264                 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_USHORT4
265                                                         : IMAGE_DATA_TYPE_USHORT;
266         }
267         else {
268                 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4
269                                                         : IMAGE_DATA_TYPE_BYTE;
270         }
271
272         in->close();
273
274         return true;
275 }
276
277 static bool image_equals(ImageManager::Image *image,
278                          const string& filename,
279                          void *builtin_data,
280                          InterpolationType interpolation,
281                          ExtensionType extension,
282                          bool use_alpha)
283 {
284         return image->filename == filename &&
285                image->builtin_data == builtin_data &&
286                image->interpolation == interpolation &&
287                image->extension == extension &&
288                image->use_alpha == use_alpha;
289 }
290
291 int ImageManager::add_image(const string& filename,
292                             void *builtin_data,
293                             bool animated,
294                             float frame,
295                             InterpolationType interpolation,
296                             ExtensionType extension,
297                             bool use_alpha,
298                             ImageMetaData& metadata)
299 {
300         Image *img;
301         size_t slot;
302
303         get_image_metadata(filename, builtin_data, metadata);
304         ImageDataType type = metadata.type;
305
306         thread_scoped_lock device_lock(device_mutex);
307
308         /* No half textures on OpenCL, use full float instead. */
309         if(!has_half_images) {
310                 if(type == IMAGE_DATA_TYPE_HALF4) {
311                         type = IMAGE_DATA_TYPE_FLOAT4;
312                 }
313                 else if(type == IMAGE_DATA_TYPE_HALF) {
314                         type = IMAGE_DATA_TYPE_FLOAT;
315                 }
316         }
317
318         /* Fnd existing image. */
319         for(slot = 0; slot < images[type].size(); slot++) {
320                 img = images[type][slot];
321                 if(img && image_equals(img,
322                                        filename,
323                                        builtin_data,
324                                        interpolation,
325                                        extension,
326                                        use_alpha))
327                 {
328                         if(img->frame != frame) {
329                                 img->frame = frame;
330                                 img->need_load = true;
331                         }
332                         if(img->use_alpha != use_alpha) {
333                                 img->use_alpha = use_alpha;
334                                 img->need_load = true;
335                         }
336                         img->users++;
337                         return type_index_to_flattened_slot(slot, type);
338                 }
339         }
340
341         /* Find free slot. */
342         for(slot = 0; slot < images[type].size(); slot++) {
343                 if(!images[type][slot])
344                         break;
345         }
346
347         /* Count if we're over the limit.
348          * Very unlikely, since max_num_images is insanely big. But better safe
349          * than sorry.
350          */
351         int tex_count = 0;
352         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
353                 tex_count += tex_num_images[type];
354         }
355         if(tex_count > max_num_images) {
356                 printf("ImageManager::add_image: Reached image limit (%d), "
357                        "skipping '%s'\n", max_num_images, filename.c_str());
358                 return -1;
359         }
360
361         if(slot == images[type].size()) {
362                 images[type].resize(images[type].size() + 1);
363         }
364
365         /* Add new image. */
366         img = new Image();
367         img->filename = filename;
368         img->builtin_data = builtin_data;
369         img->metadata = metadata;
370         img->need_load = true;
371         img->animated = animated;
372         img->frame = frame;
373         img->interpolation = interpolation;
374         img->extension = extension;
375         img->users = 1;
376         img->use_alpha = use_alpha;
377         img->mem = NULL;
378
379         images[type][slot] = img;
380
381         ++tex_num_images[type];
382
383         need_update = true;
384
385         return type_index_to_flattened_slot(slot, type);
386 }
387
388 void ImageManager::remove_image(int flat_slot)
389 {
390         ImageDataType type;
391         int slot = flattened_slot_to_type_index(flat_slot, &type);
392
393         Image *image = images[type][slot];
394         assert(image && image->users >= 1);
395
396         /* decrement user count */
397         image->users--;
398
399         /* don't remove immediately, rather do it all together later on. one of
400          * the reasons for this is that on shader changes we add and remove nodes
401          * that use them, but we do not want to reload the image all the time. */
402         if(image->users == 0)
403                 need_update = true;
404 }
405
406 void ImageManager::remove_image(const string& filename,
407                                 void *builtin_data,
408                                 InterpolationType interpolation,
409                                 ExtensionType extension,
410                                 bool use_alpha)
411 {
412         size_t slot;
413
414         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
415                 for(slot = 0; slot < images[type].size(); slot++) {
416                         if(images[type][slot] && image_equals(images[type][slot],
417                                                               filename,
418                                                               builtin_data,
419                                                               interpolation,
420                                                               extension,
421                                                               use_alpha))
422                         {
423                                 remove_image(type_index_to_flattened_slot(slot, (ImageDataType)type));
424                                 return;
425                         }
426                 }
427         }
428 }
429
430 /* TODO(sergey): Deduplicate with the iteration above, but make it pretty,
431  * without bunch of arguments passing around making code readability even
432  * more cluttered.
433  */
434 void ImageManager::tag_reload_image(const string& filename,
435                                     void *builtin_data,
436                                     InterpolationType interpolation,
437                                     ExtensionType extension,
438                                     bool use_alpha)
439 {
440         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
441                 for(size_t slot = 0; slot < images[type].size(); slot++) {
442                         if(images[type][slot] && image_equals(images[type][slot],
443                                                               filename,
444                                                               builtin_data,
445                                                               interpolation,
446                                                               extension,
447                                                               use_alpha))
448                         {
449                                 images[type][slot]->need_load = true;
450                                 break;
451                         }
452                 }
453         }
454 }
455
456 bool ImageManager::file_load_image_generic(Image *img,
457                                            unique_ptr<ImageInput> *in)
458 {
459         if(img->filename == "")
460                 return false;
461
462         if(!img->builtin_data) {
463                 /* NOTE: Error logging is done in meta data acquisition. */
464                 if(!path_exists(img->filename) || path_is_directory(img->filename)) {
465                         return false;
466                 }
467
468                 /* load image from file through OIIO */
469                 *in = unique_ptr<ImageInput>(ImageInput::create(img->filename));
470
471                 if(!*in)
472                         return false;
473
474                 ImageSpec spec = ImageSpec();
475                 ImageSpec config = ImageSpec();
476
477                 if(img->use_alpha == false)
478                         config.attribute("oiio:UnassociatedAlpha", 1);
479
480                 if(!(*in)->open(img->filename, spec, config)) {
481                         return false;
482                 }
483         }
484         else {
485                 /* load image using builtin images callbacks */
486                 if(!builtin_image_info_cb || !builtin_image_pixels_cb)
487                         return false;
488         }
489
490         /* we only handle certain number of components */
491         if(!(img->metadata.channels >= 1 && img->metadata.channels <= 4)) {
492                 if(*in) {
493                         (*in)->close();
494                 }
495                 return false;
496         }
497
498         return true;
499 }
500
501 template<TypeDesc::BASETYPE FileFormat,
502          typename StorageType,
503          typename DeviceType>
504 bool ImageManager::file_load_image(Image *img,
505                                    ImageDataType type,
506                                    int texture_limit,
507                                    device_vector<DeviceType>& tex_img)
508 {
509         unique_ptr<ImageInput> in = NULL;
510         if(!file_load_image_generic(img, &in)) {
511                 return false;
512         }
513
514         /* Get metadata. */
515         int width = img->metadata.width;
516         int height = img->metadata.height;
517         int depth = img->metadata.depth;
518         int components = img->metadata.channels;
519
520         /* Read RGBA pixels. */
521         vector<StorageType> pixels_storage;
522         StorageType *pixels;
523         const size_t max_size = max(max(width, height), depth);
524         if(max_size == 0) {
525                 /* Don't bother with invalid images. */
526                 return false;
527         }
528         if(texture_limit > 0 && max_size > texture_limit) {
529                 pixels_storage.resize(((size_t)width)*height*depth*4);
530                 pixels = &pixels_storage[0];
531         }
532         else {
533                 thread_scoped_lock device_lock(device_mutex);
534                 pixels = (StorageType*)tex_img.alloc(width, height, depth);
535         }
536         if(pixels == NULL) {
537                 /* Could be that we've run out of memory. */
538                 return false;
539         }
540         bool cmyk = false;
541         const size_t num_pixels = ((size_t)width) * height * depth;
542         if(in) {
543                 StorageType *readpixels = pixels;
544                 vector<StorageType> tmppixels;
545                 if(components > 4) {
546                         tmppixels.resize(((size_t)width)*height*components);
547                         readpixels = &tmppixels[0];
548                 }
549                 if(depth <= 1) {
550                         size_t scanlinesize = ((size_t)width)*components*sizeof(StorageType);
551                         in->read_image(FileFormat,
552                                        (uchar*)readpixels + (height-1)*scanlinesize,
553                                        AutoStride,
554                                        -scanlinesize,
555                                        AutoStride);
556                 }
557                 else {
558                         in->read_image(FileFormat, (uchar*)readpixels);
559                 }
560                 if(components > 4) {
561                         size_t dimensions = ((size_t)width)*height;
562                         for(size_t i = dimensions-1, pixel = 0; pixel < dimensions; pixel++, i--) {
563                                 pixels[i*4+3] = tmppixels[i*components+3];
564                                 pixels[i*4+2] = tmppixels[i*components+2];
565                                 pixels[i*4+1] = tmppixels[i*components+1];
566                                 pixels[i*4+0] = tmppixels[i*components+0];
567                         }
568                         tmppixels.clear();
569                 }
570                 cmyk = strcmp(in->format_name(), "jpeg") == 0 && components == 4;
571                 in->close();
572         }
573         else {
574                 if(FileFormat == TypeDesc::FLOAT) {
575                         builtin_image_float_pixels_cb(img->filename,
576                                                       img->builtin_data,
577                                                       (float*)&pixels[0],
578                                                       num_pixels * components,
579                                                       img->metadata.builtin_free_cache);
580                 }
581                 else if(FileFormat == TypeDesc::UINT8) {
582                         builtin_image_pixels_cb(img->filename,
583                                                 img->builtin_data,
584                                                 (uchar*)&pixels[0],
585                                                 num_pixels * components,
586                                                 img->metadata.builtin_free_cache);
587                 }
588                 else {
589                         /* TODO(dingto): Support half for ImBuf. */
590                 }
591         }
592         /* Check if we actually have a float4 slot, in case components == 1,
593          * but device doesn't support single channel textures.
594          */
595         bool is_rgba = (type == IMAGE_DATA_TYPE_FLOAT4 ||
596                         type == IMAGE_DATA_TYPE_HALF4 ||
597                         type == IMAGE_DATA_TYPE_BYTE4 ||
598                                         type == IMAGE_DATA_TYPE_USHORT4);
599         if(is_rgba) {
600                 const StorageType one = util_image_cast_from_float<StorageType>(1.0f);
601
602                 if(cmyk) {
603                         /* CMYK */
604                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
605                                 float c = util_image_cast_to_float(pixels[i*4+0]);
606                                 float m = util_image_cast_to_float(pixels[i*4+1]);
607                                 float y = util_image_cast_to_float(pixels[i*4+2]);
608                                 float k = util_image_cast_to_float(pixels[i*4+3]);
609                                 pixels[i*4+0] = util_image_cast_from_float<StorageType>((1.0f - c) * (1.0f - k));
610                                 pixels[i*4+1] = util_image_cast_from_float<StorageType>((1.0f - m) * (1.0f - k));
611                                 pixels[i*4+2] = util_image_cast_from_float<StorageType>((1.0f - y) * (1.0f - k));
612                                 pixels[i*4+3] = one;
613                         }
614                 }
615                 else if(components == 2) {
616                         /* grayscale + alpha */
617                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
618                                 pixels[i*4+3] = pixels[i*2+1];
619                                 pixels[i*4+2] = pixels[i*2+0];
620                                 pixels[i*4+1] = pixels[i*2+0];
621                                 pixels[i*4+0] = pixels[i*2+0];
622                         }
623                 }
624                 else if(components == 3) {
625                         /* RGB */
626                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
627                                 pixels[i*4+3] = one;
628                                 pixels[i*4+2] = pixels[i*3+2];
629                                 pixels[i*4+1] = pixels[i*3+1];
630                                 pixels[i*4+0] = pixels[i*3+0];
631                         }
632                 }
633                 else if(components == 1) {
634                         /* grayscale */
635                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
636                                 pixels[i*4+3] = one;
637                                 pixels[i*4+2] = pixels[i];
638                                 pixels[i*4+1] = pixels[i];
639                                 pixels[i*4+0] = pixels[i];
640                         }
641                 }
642                 if(img->use_alpha == false) {
643                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
644                                 pixels[i*4+3] = one;
645                         }
646                 }
647         }
648         /* Make sure we don't have buggy values. */
649         if(FileFormat == TypeDesc::FLOAT) {
650                 /* For RGBA buffers we put all channels to 0 if either of them is not
651                  * finite. This way we avoid possible artifacts caused by fully changed
652                  * hue.
653                  */
654                 if(is_rgba) {
655                         for(size_t i = 0; i < num_pixels; i += 4) {
656                                 StorageType *pixel = &pixels[i*4];
657                                 if(!isfinite(pixel[0]) ||
658                                    !isfinite(pixel[1]) ||
659                                    !isfinite(pixel[2]) ||
660                                    !isfinite(pixel[3]))
661                                 {
662                                         pixel[0] = 0;
663                                         pixel[1] = 0;
664                                         pixel[2] = 0;
665                                         pixel[3] = 0;
666                                 }
667                         }
668                 }
669                 else {
670                         for(size_t i = 0; i < num_pixels; ++i) {
671                                 StorageType *pixel = &pixels[i];
672                                 if(!isfinite(pixel[0])) {
673                                         pixel[0] = 0;
674                                 }
675                         }
676                 }
677         }
678         /* Scale image down if needed. */
679         if(pixels_storage.size() > 0) {
680                 float scale_factor = 1.0f;
681                 while(max_size * scale_factor > texture_limit) {
682                         scale_factor *= 0.5f;
683                 }
684                 VLOG(1) << "Scaling image " << img->filename
685                         << " by a factor of " << scale_factor << ".";
686                 vector<StorageType> scaled_pixels;
687                 size_t scaled_width, scaled_height, scaled_depth;
688                 util_image_resize_pixels(pixels_storage,
689                                          width, height, depth,
690                                          is_rgba ? 4 : 1,
691                                          scale_factor,
692                                          &scaled_pixels,
693                                          &scaled_width, &scaled_height, &scaled_depth);
694
695                 StorageType *texture_pixels;
696
697                 {
698                         thread_scoped_lock device_lock(device_mutex);
699                         texture_pixels = (StorageType*)tex_img.alloc(scaled_width,
700                                                                      scaled_height,
701                                                                      scaled_depth);
702                 }
703
704                 memcpy(texture_pixels,
705                        &scaled_pixels[0],
706                        scaled_pixels.size() * sizeof(StorageType));
707         }
708         return true;
709 }
710
711 void ImageManager::device_load_image(Device *device,
712                                      Scene *scene,
713                                      ImageDataType type,
714                                      int slot,
715                                      Progress *progress)
716 {
717         if(progress->get_cancel())
718                 return;
719
720         Image *img = images[type][slot];
721
722         if(osl_texture_system && !img->builtin_data)
723                 return;
724
725         string filename = path_filename(images[type][slot]->filename);
726         progress->set_status("Updating Images", "Loading " + filename);
727
728         const int texture_limit = scene->params.texture_limit;
729
730         /* Slot assignment */
731         int flat_slot = type_index_to_flattened_slot(slot, type);
732         img->mem_name = string_printf("__tex_image_%s_%03d",
733                                       name_from_type(type), flat_slot);
734
735         /* Free previous texture in slot. */
736         if(img->mem) {
737                 thread_scoped_lock device_lock(device_mutex);
738                 delete img->mem;
739                 img->mem = NULL;
740         }
741
742         /* Create new texture. */
743         if(type == IMAGE_DATA_TYPE_FLOAT4) {
744                 device_vector<float4> *tex_img
745                         = new device_vector<float4>(device, img->mem_name.c_str(), MEM_TEXTURE);
746
747                 if(!file_load_image<TypeDesc::FLOAT, float>(img,
748                                                             type,
749                                                             texture_limit,
750                                                             *tex_img))
751                 {
752                         /* on failure to load, we set a 1x1 pixels pink image */
753                         thread_scoped_lock device_lock(device_mutex);
754                         float *pixels = (float*)tex_img->alloc(1, 1);
755
756                         pixels[0] = TEX_IMAGE_MISSING_R;
757                         pixels[1] = TEX_IMAGE_MISSING_G;
758                         pixels[2] = TEX_IMAGE_MISSING_B;
759                         pixels[3] = TEX_IMAGE_MISSING_A;
760                 }
761
762                 img->mem = tex_img;
763                 img->mem->interpolation = img->interpolation;
764                 img->mem->extension = img->extension;
765
766                 thread_scoped_lock device_lock(device_mutex);
767                 tex_img->copy_to_device();
768         }
769         else if(type == IMAGE_DATA_TYPE_FLOAT) {
770                 device_vector<float> *tex_img
771                         = new device_vector<float>(device, img->mem_name.c_str(), MEM_TEXTURE);
772
773                 if(!file_load_image<TypeDesc::FLOAT, float>(img,
774                                                             type,
775                                                             texture_limit,
776                                                             *tex_img))
777                 {
778                         /* on failure to load, we set a 1x1 pixels pink image */
779                         thread_scoped_lock device_lock(device_mutex);
780                         float *pixels = (float*)tex_img->alloc(1, 1);
781
782                         pixels[0] = TEX_IMAGE_MISSING_R;
783                 }
784
785                 img->mem = tex_img;
786                 img->mem->interpolation = img->interpolation;
787                 img->mem->extension = img->extension;
788
789                 thread_scoped_lock device_lock(device_mutex);
790                 tex_img->copy_to_device();
791         }
792         else if(type == IMAGE_DATA_TYPE_BYTE4) {
793                 device_vector<uchar4> *tex_img
794                         = new device_vector<uchar4>(device, img->mem_name.c_str(), MEM_TEXTURE);
795
796                 if(!file_load_image<TypeDesc::UINT8, uchar>(img,
797                                                             type,
798                                                             texture_limit,
799                                                             *tex_img))
800                 {
801                         /* on failure to load, we set a 1x1 pixels pink image */
802                         thread_scoped_lock device_lock(device_mutex);
803                         uchar *pixels = (uchar*)tex_img->alloc(1, 1);
804
805                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
806                         pixels[1] = (TEX_IMAGE_MISSING_G * 255);
807                         pixels[2] = (TEX_IMAGE_MISSING_B * 255);
808                         pixels[3] = (TEX_IMAGE_MISSING_A * 255);
809                 }
810
811                 img->mem = tex_img;
812                 img->mem->interpolation = img->interpolation;
813                 img->mem->extension = img->extension;
814
815                 thread_scoped_lock device_lock(device_mutex);
816                 tex_img->copy_to_device();
817         }
818         else if(type == IMAGE_DATA_TYPE_BYTE) {
819                 device_vector<uchar> *tex_img
820                         = new device_vector<uchar>(device, img->mem_name.c_str(), MEM_TEXTURE);
821
822                 if(!file_load_image<TypeDesc::UINT8, uchar>(img,
823                                                             type,
824                                                             texture_limit,
825                                                             *tex_img)) {
826                         /* on failure to load, we set a 1x1 pixels pink image */
827                         thread_scoped_lock device_lock(device_mutex);
828                         uchar *pixels = (uchar*)tex_img->alloc(1, 1);
829
830                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
831                 }
832
833                 img->mem = tex_img;
834                 img->mem->interpolation = img->interpolation;
835                 img->mem->extension = img->extension;
836
837                 thread_scoped_lock device_lock(device_mutex);
838                 tex_img->copy_to_device();
839         }
840         else if(type == IMAGE_DATA_TYPE_HALF4) {
841                 device_vector<half4> *tex_img
842                         = new device_vector<half4>(device, img->mem_name.c_str(), MEM_TEXTURE);
843
844                 if(!file_load_image<TypeDesc::HALF, half>(img,
845                                                           type,
846                                                           texture_limit,
847                                                           *tex_img)) {
848                         /* on failure to load, we set a 1x1 pixels pink image */
849                         thread_scoped_lock device_lock(device_mutex);
850                         half *pixels = (half*)tex_img->alloc(1, 1);
851
852                         pixels[0] = TEX_IMAGE_MISSING_R;
853                         pixels[1] = TEX_IMAGE_MISSING_G;
854                         pixels[2] = TEX_IMAGE_MISSING_B;
855                         pixels[3] = TEX_IMAGE_MISSING_A;
856                 }
857
858                 img->mem = tex_img;
859                 img->mem->interpolation = img->interpolation;
860                 img->mem->extension = img->extension;
861
862                 thread_scoped_lock device_lock(device_mutex);
863                 tex_img->copy_to_device();
864         }
865         else if(type == IMAGE_DATA_TYPE_USHORT) {
866                 device_vector<uint16_t> *tex_img
867                         = new device_vector<uint16_t>(device, img->mem_name.c_str(), MEM_TEXTURE);
868
869                 if(!file_load_image<TypeDesc::USHORT, uint16_t>(img,
870                                                           type,
871                                                           texture_limit,
872                                                           *tex_img)) {
873                         /* on failure to load, we set a 1x1 pixels pink image */
874                         thread_scoped_lock device_lock(device_mutex);
875                         uint16_t *pixels = (uint16_t*)tex_img->alloc(1, 1);
876
877                         pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
878                 }
879
880                 img->mem = tex_img;
881                 img->mem->interpolation = img->interpolation;
882                 img->mem->extension = img->extension;
883
884                 thread_scoped_lock device_lock(device_mutex);
885                 tex_img->copy_to_device();
886         }
887         else if(type == IMAGE_DATA_TYPE_USHORT4) {
888                 device_vector<ushort4> *tex_img
889                         = new device_vector<ushort4>(device, img->mem_name.c_str(), MEM_TEXTURE);
890
891                 if(!file_load_image<TypeDesc::USHORT, uint16_t>(img,
892                         type,
893                         texture_limit,
894                         *tex_img)) {
895                         /* on failure to load, we set a 1x1 pixels pink image */
896                         thread_scoped_lock device_lock(device_mutex);
897                         uint16_t *pixels = (uint16_t*)tex_img->alloc(1, 1);
898
899                         pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
900                         pixels[1] = (TEX_IMAGE_MISSING_G * 65535);
901                         pixels[2] = (TEX_IMAGE_MISSING_B * 65535);
902                         pixels[3] = (TEX_IMAGE_MISSING_A * 65535);
903                 }
904
905                 img->mem = tex_img;
906                 img->mem->interpolation = img->interpolation;
907                 img->mem->extension = img->extension;
908
909                 thread_scoped_lock device_lock(device_mutex);
910                 tex_img->copy_to_device();
911         }
912         else if(type == IMAGE_DATA_TYPE_HALF) {
913                 device_vector<half> *tex_img
914                         = new device_vector<half>(device, img->mem_name.c_str(), MEM_TEXTURE);
915
916                 if(!file_load_image<TypeDesc::HALF, half>(img,
917                         type,
918                         texture_limit,
919                         *tex_img)) {
920                         /* on failure to load, we set a 1x1 pixels pink image */
921                         thread_scoped_lock device_lock(device_mutex);
922                         half *pixels = (half*)tex_img->alloc(1, 1);
923
924                         pixels[0] = TEX_IMAGE_MISSING_R;
925                 }
926
927                 img->mem = tex_img;
928                 img->mem->interpolation = img->interpolation;
929                 img->mem->extension = img->extension;
930
931                 thread_scoped_lock device_lock(device_mutex);
932                 tex_img->copy_to_device();
933         }
934         img->need_load = false;
935 }
936
937 void ImageManager::device_free_image(Device *, ImageDataType type, int slot)
938 {
939         Image *img = images[type][slot];
940
941         if(img) {
942                 if(osl_texture_system && !img->builtin_data) {
943 #ifdef WITH_OSL
944                         ustring filename(images[type][slot]->filename);
945                         ((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
946 #endif
947                 }
948
949                 if(img->mem) {
950                         thread_scoped_lock device_lock(device_mutex);
951                         delete img->mem;
952                 }
953
954                 delete img;
955                 images[type][slot] = NULL;
956                 --tex_num_images[type];
957         }
958 }
959
960 void ImageManager::device_update(Device *device,
961                                  Scene *scene,
962                                  Progress& progress)
963 {
964         if(!need_update) {
965                 return;
966         }
967
968         TaskPool pool;
969         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
970                 for(size_t slot = 0; slot < images[type].size(); slot++) {
971                         if(!images[type][slot])
972                                 continue;
973
974                         if(images[type][slot]->users == 0) {
975                                 device_free_image(device, (ImageDataType)type, slot);
976                         }
977                         else if(images[type][slot]->need_load) {
978                                 if(!osl_texture_system || images[type][slot]->builtin_data)
979                                         pool.push(function_bind(&ImageManager::device_load_image,
980                                                                 this,
981                                                                 device,
982                                                                 scene,
983                                                                 (ImageDataType)type,
984                                                                 slot,
985                                                                 &progress));
986                         }
987                 }
988         }
989
990         pool.wait_work();
991
992         need_update = false;
993 }
994
995 void ImageManager::device_update_slot(Device *device,
996                                       Scene *scene,
997                                       int flat_slot,
998                                       Progress *progress)
999 {
1000         ImageDataType type;
1001         int slot = flattened_slot_to_type_index(flat_slot, &type);
1002
1003         Image *image = images[type][slot];
1004         assert(image != NULL);
1005
1006         if(image->users == 0) {
1007                 device_free_image(device, type, slot);
1008         }
1009         else if(image->need_load) {
1010                 if(!osl_texture_system || image->builtin_data)
1011                         device_load_image(device,
1012                                           scene,
1013                                           type,
1014                                           slot,
1015                                           progress);
1016         }
1017 }
1018
1019 void ImageManager::device_load_builtin(Device *device,
1020                                        Scene *scene,
1021                                        Progress& progress)
1022 {
1023         /* Load only builtin images, Blender needs this to load evaluated
1024          * scene data from depsgraph before it is freed. */
1025         if(!need_update) {
1026                 return;
1027         }
1028
1029         TaskPool pool;
1030         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1031                 for(size_t slot = 0; slot < images[type].size(); slot++) {
1032                         if(!images[type][slot])
1033                                 continue;
1034
1035                         if(images[type][slot]->need_load) {
1036                                 if(images[type][slot]->builtin_data) {
1037                                         pool.push(function_bind(&ImageManager::device_load_image,
1038                                                                 this,
1039                                                                 device,
1040                                                                 scene,
1041                                                                 (ImageDataType)type,
1042                                                                 slot,
1043                                                                 &progress));
1044                                 }
1045                         }
1046                 }
1047         }
1048
1049         pool.wait_work();
1050 }
1051
1052 void ImageManager::device_free_builtin(Device *device)
1053 {
1054         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1055                 for(size_t slot = 0; slot < images[type].size(); slot++) {
1056                         if(images[type][slot] && images[type][slot]->builtin_data)
1057                                 device_free_image(device, (ImageDataType)type, slot);
1058                 }
1059         }
1060 }
1061
1062 void ImageManager::device_free(Device *device)
1063 {
1064         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1065                 for(size_t slot = 0; slot < images[type].size(); slot++) {
1066                         device_free_image(device, (ImageDataType)type, slot);
1067                 }
1068                 images[type].clear();
1069         }
1070 }
1071
1072 void ImageManager::collect_statistics(RenderStats *stats)
1073 {
1074         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1075                 foreach(const Image *image, images[type]) {
1076                         stats->image.textures.add_entry(
1077                                 NamedSizeEntry(path_filename(image->filename),
1078                                                image->mem->memory_size()));
1079                 }
1080         }
1081 }
1082
1083 CCL_NAMESPACE_END