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