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