Hide object center and other info when drawing sets
[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 ImageManager::ImageManager(const DeviceInfo& info)
34 {
35         need_update = true;
36         pack_images = false;
37         osl_texture_system = NULL;
38         animation_frame = 0;
39
40         /* In case of multiple devices used we need to know type of an actual
41          * compute device.
42          *
43          * NOTE: We assume that all the devices are same type, otherwise we'll
44          * be screwed on so many levels..
45          */
46         DeviceType device_type = info.type;
47         if(device_type == DEVICE_MULTI) {
48                 device_type = info.multi_devices[0].type;
49         }
50
51         /* Set image limits */
52         max_num_images = TEX_NUM_MAX;
53         has_half_images = true;
54         cuda_fermi_limits = false;
55         
56         if(device_type == DEVICE_CUDA) {
57                 if(!info.has_bindless_textures) {
58                         /* CUDA Fermi hardware (SM 2.x) has a hard limit on the number of textures */
59                         cuda_fermi_limits = true;
60                         has_half_images = false;
61                 }
62         }
63         else if(device_type == DEVICE_OPENCL) {
64                 has_half_images = false;
65         }
66         
67         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
68                 tex_num_images[type] = 0;
69         }
70 }
71
72 ImageManager::~ImageManager()
73 {
74         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
75                 for(size_t slot = 0; slot < images[type].size(); slot++)
76                         assert(!images[type][slot]);
77         }
78 }
79
80 void ImageManager::set_pack_images(bool pack_images_)
81 {
82         pack_images = pack_images_;
83 }
84
85 void ImageManager::set_osl_texture_system(void *texture_system)
86 {
87         osl_texture_system = texture_system;
88 }
89
90 bool ImageManager::set_animation_frame_update(int frame)
91 {
92         if(frame != animation_frame) {
93                 animation_frame = frame;
94
95                 for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
96                         for(size_t slot = 0; slot < images[type].size(); slot++) {
97                                 if(images[type][slot] && images[type][slot]->animated)
98                                         return true;
99                         }
100                 }
101         }
102
103         return false;
104 }
105
106 ImageDataType ImageManager::get_image_metadata(const string& filename,
107                                                              void *builtin_data,
108                                                              bool& is_linear)
109 {
110         bool is_float = false, is_half = false;
111         is_linear = false;
112         int channels = 4;
113
114         if(builtin_data) {
115                 if(builtin_image_info_cb) {
116                         int width, height, depth;
117                         builtin_image_info_cb(filename, builtin_data, is_float, width, height, depth, channels);
118                 }
119
120                 if(is_float) {
121                         is_linear = true;
122                         return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
123                 }
124                 else {
125                         return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
126                 }
127         }
128
129         /* Perform preliminary checks, with meaningful logging. */
130         if(!path_exists(filename)) {
131                 VLOG(1) << "File '" << filename << "' does not exist.";
132                 return IMAGE_DATA_TYPE_BYTE4;
133         }
134         if(path_is_directory(filename)) {
135                 VLOG(1) << "File '" << filename << "' is a directory, can't use as image.";
136                 return IMAGE_DATA_TYPE_BYTE4;
137         }
138
139         ImageInput *in = ImageInput::create(filename);
140
141         if(in) {
142                 ImageSpec spec;
143
144                 if(in->open(filename, spec)) {
145                         /* check the main format, and channel formats;
146                          * if any take up more than one byte, we'll need a float texture slot */
147                         if(spec.format.basesize() > 1) {
148                                 is_float = true;
149                                 is_linear = true;
150                         }
151
152                         for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
153                                 if(spec.channelformats[channel].basesize() > 1) {
154                                         is_float = true;
155                                         is_linear = true;
156                                 }
157                         }
158
159                         /* check if it's half float */
160                         if(spec.format == TypeDesc::HALF)
161                                 is_half = true;
162
163                         channels = spec.nchannels;
164
165                         /* basic color space detection, not great but better than nothing
166                          * before we do OpenColorIO integration */
167                         if(is_float) {
168                                 string colorspace = spec.get_string_attribute("oiio:ColorSpace");
169
170                                 is_linear = !(colorspace == "sRGB" ||
171                                               colorspace == "GammaCorrected" ||
172                                               (colorspace == "" &&
173                                                   (strcmp(in->format_name(), "png") == 0 ||
174                                                    strcmp(in->format_name(), "tiff") == 0 ||
175                                                    strcmp(in->format_name(), "dpx") == 0 ||
176                                                    strcmp(in->format_name(), "jpeg2000") == 0)));
177                         }
178                         else {
179                                 is_linear = false;
180                         }
181
182                         in->close();
183                 }
184
185                 delete in;
186         }
187
188         if(is_half) {
189                 return (channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
190         }
191         else if(is_float) {
192                 return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
193         }
194         else {
195                 return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
196         }
197 }
198
199 /* The lower three bits of a device texture slot number indicate its type.
200  * These functions convert the slot ids from ImageManager "images" ones
201  * to device ones and vice versa.
202  *
203  * There are special cases for CUDA Fermi, since there we have only 90 image texture
204  * slots available and shold keep the flattended numbers in the 0-89 range.
205  */
206 int ImageManager::type_index_to_flattened_slot(int slot, ImageDataType type)
207 {
208         if(cuda_fermi_limits) {
209                 if(type == IMAGE_DATA_TYPE_BYTE4) {
210                         return slot + TEX_START_BYTE4_CUDA;
211                 }
212                 else {
213                         return slot;
214                 }
215         }
216
217         return (slot << IMAGE_DATA_TYPE_SHIFT) | (type);
218 }
219
220 int ImageManager::flattened_slot_to_type_index(int flat_slot, ImageDataType *type)
221 {
222         if(cuda_fermi_limits) {
223                 if(flat_slot >= 4) {
224                         *type = IMAGE_DATA_TYPE_BYTE4;
225                         return flat_slot - TEX_START_BYTE4_CUDA;
226                 }
227                 else {
228                         *type = IMAGE_DATA_TYPE_FLOAT4;
229                         return flat_slot;
230                 }
231         }
232
233         *type = (ImageDataType)(flat_slot & IMAGE_DATA_TYPE_MASK);
234         return flat_slot >> IMAGE_DATA_TYPE_SHIFT;
235 }
236
237 string ImageManager::name_from_type(int type)
238 {
239         if(type == IMAGE_DATA_TYPE_FLOAT4)
240                 return "float4";
241         else if(type == IMAGE_DATA_TYPE_FLOAT)
242                 return "float";
243         else if(type == IMAGE_DATA_TYPE_BYTE)
244                 return "byte";
245         else if(type == IMAGE_DATA_TYPE_HALF4)
246                 return "half4";
247         else if(type == IMAGE_DATA_TYPE_HALF)
248                 return "half";
249         else
250                 return "byte4";
251 }
252
253 static bool image_equals(ImageManager::Image *image,
254                          const string& filename,
255                          void *builtin_data,
256                          InterpolationType interpolation,
257                          ExtensionType extension,
258                          bool use_alpha)
259 {
260         return image->filename == filename &&
261                image->builtin_data == builtin_data &&
262                image->interpolation == interpolation &&
263                image->extension == extension &&
264                image->use_alpha == use_alpha;
265 }
266
267 int ImageManager::add_image(const string& filename,
268                             void *builtin_data,
269                             bool animated,
270                             float frame,
271                             bool& is_float,
272                             bool& is_linear,
273                             InterpolationType interpolation,
274                             ExtensionType extension,
275                             bool use_alpha)
276 {
277         Image *img;
278         size_t slot;
279
280         ImageDataType type = get_image_metadata(filename, builtin_data, is_linear);
281
282         thread_scoped_lock device_lock(device_mutex);
283
284         /* Check whether it's a float texture. */
285         is_float = (type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4);
286
287         /* No single channel and half textures on CUDA (Fermi) and no half on OpenCL, use available slots */
288         if(!has_half_images) {
289                 if(type == IMAGE_DATA_TYPE_HALF4) {
290                         type = IMAGE_DATA_TYPE_FLOAT4;
291                 }
292                 else if(type == IMAGE_DATA_TYPE_HALF) {
293                         type = IMAGE_DATA_TYPE_FLOAT;
294                 }
295         }
296
297         if(cuda_fermi_limits) {
298                 if(type == IMAGE_DATA_TYPE_FLOAT) {
299                         type = IMAGE_DATA_TYPE_FLOAT4;
300                 }
301                 else if(type == IMAGE_DATA_TYPE_BYTE) {
302                         type = IMAGE_DATA_TYPE_BYTE4;
303                 }
304         }
305
306         /* Fnd existing image. */
307         for(slot = 0; slot < images[type].size(); slot++) {
308                 img = images[type][slot];
309                 if(img && image_equals(img,
310                                        filename,
311                                        builtin_data,
312                                        interpolation,
313                                        extension,
314                                        use_alpha))
315                 {
316                         if(img->frame != frame) {
317                                 img->frame = frame;
318                                 img->need_load = true;
319                         }
320                         if(img->use_alpha != use_alpha) {
321                                 img->use_alpha = use_alpha;
322                                 img->need_load = true;
323                         }
324                         img->users++;
325                         return type_index_to_flattened_slot(slot, type);
326                 }
327         }
328
329         /* Find free slot. */
330         for(slot = 0; slot < images[type].size(); slot++) {
331                 if(!images[type][slot])
332                         break;
333         }
334
335         /* Count if we're over the limit */
336         if(cuda_fermi_limits) {
337                 if(tex_num_images[IMAGE_DATA_TYPE_BYTE4] == TEX_NUM_BYTE4_CUDA
338                         || tex_num_images[IMAGE_DATA_TYPE_FLOAT4] == TEX_NUM_FLOAT4_CUDA)
339                 {
340                         printf("ImageManager::add_image: Reached %s image limit (%d), skipping '%s'\n",
341                                 name_from_type(type).c_str(), tex_num_images[type], filename.c_str());
342                         return -1;
343                 }
344         }
345         else {
346                 /* Very unlikely, since max_num_images is insanely big. But better safe than sorry. */
347                 int tex_count = 0;
348                 for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
349                         tex_count += tex_num_images[type];
350                 }
351                 if(tex_count > max_num_images) {
352                         printf("ImageManager::add_image: Reached image limit (%d), skipping '%s'\n",
353                                 max_num_images, filename.c_str());
354                         return -1;
355                 }
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->need_load = true;
367         img->animated = animated;
368         img->frame = frame;
369         img->interpolation = interpolation;
370         img->extension = extension;
371         img->users = 1;
372         img->use_alpha = use_alpha;
373
374         images[type][slot] = img;
375         
376         ++tex_num_images[type];
377
378         need_update = true;
379
380         return type_index_to_flattened_slot(slot, type);
381 }
382
383 void ImageManager::remove_image(int flat_slot)
384 {
385         ImageDataType type;
386         int slot = flattened_slot_to_type_index(flat_slot, &type);
387
388         Image *image = images[type][slot];
389         assert(image && image->users >= 1);
390
391         /* decrement user count */
392         image->users--;
393
394         /* don't remove immediately, rather do it all together later on. one of
395          * the reasons for this is that on shader changes we add and remove nodes
396          * that use them, but we do not want to reload the image all the time. */
397         if(image->users == 0)
398                 need_update = true;
399 }
400
401 void ImageManager::remove_image(const string& filename,
402                                 void *builtin_data,
403                                 InterpolationType interpolation,
404                                 ExtensionType extension,
405                                 bool use_alpha)
406 {
407         size_t slot;
408
409         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
410                 for(slot = 0; slot < images[type].size(); slot++) {
411                         if(images[type][slot] && image_equals(images[type][slot],
412                                                               filename,
413                                                               builtin_data,
414                                                               interpolation,
415                                                               extension,
416                                                               use_alpha))
417                         {
418                                 remove_image(type_index_to_flattened_slot(slot, (ImageDataType)type));
419                                 return;
420                         }
421                 }
422         }
423 }
424
425 /* TODO(sergey): Deduplicate with the iteration above, but make it pretty,
426  * without bunch of arguments passing around making code readability even
427  * more cluttered.
428  */
429 void ImageManager::tag_reload_image(const string& filename,
430                                     void *builtin_data,
431                                     InterpolationType interpolation,
432                                     ExtensionType extension,
433                                     bool use_alpha)
434 {
435         for(size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
436                 for(size_t slot = 0; slot < images[type].size(); slot++) {
437                         if(images[type][slot] && image_equals(images[type][slot],
438                                                               filename,
439                                                               builtin_data,
440                                                               interpolation,
441                                                               extension,
442                                                               use_alpha))
443                         {
444                                 images[type][slot]->need_load = true;
445                                 break;
446                         }
447                 }
448         }
449 }
450
451 bool ImageManager::file_load_image_generic(Image *img, ImageInput **in, int &width, int &height, int &depth, int &components)
452 {
453         if(img->filename == "")
454                 return false;
455
456         if(!img->builtin_data) {
457                 /* NOTE: Error logging is done in meta data acquisition. */
458                 if(!path_exists(img->filename) || path_is_directory(img->filename)) {
459                         return false;
460                 }
461
462                 /* load image from file through OIIO */
463                 *in = ImageInput::create(img->filename);
464
465                 if(!*in)
466                         return false;
467
468                 ImageSpec spec = ImageSpec();
469                 ImageSpec config = ImageSpec();
470
471                 if(img->use_alpha == false)
472                         config.attribute("oiio:UnassociatedAlpha", 1);
473
474                 if(!(*in)->open(img->filename, spec, config)) {
475                         delete *in;
476                         *in = NULL;
477                         return false;
478                 }
479
480                 width = spec.width;
481                 height = spec.height;
482                 depth = spec.depth;
483                 components = spec.nchannels;
484         }
485         else {
486                 /* load image using builtin images callbacks */
487                 if(!builtin_image_info_cb || !builtin_image_pixels_cb)
488                         return false;
489
490                 bool is_float;
491                 builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, depth, components);
492         }
493
494         /* we only handle certain number of components */
495         if(!(components >= 1 && components <= 4)) {
496                 if(*in) {
497                         (*in)->close();
498                         delete *in;
499                         *in = NULL;
500                 }
501
502                 return false;
503         }
504
505         return true;
506 }
507
508 template<TypeDesc::BASETYPE FileFormat,
509          typename StorageType,
510          typename DeviceType>
511 bool ImageManager::file_load_image(Image *img,
512                                    ImageDataType type,
513                                    int texture_limit,
514                                    device_vector<DeviceType>& tex_img)
515 {
516         const StorageType alpha_one = (FileFormat == TypeDesc::UINT8)? 255 : 1;
517         ImageInput *in = NULL;
518         int width, height, depth, components;
519         if(!file_load_image_generic(img, &in, width, height, depth, components)) {
520                 return false;
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(texture_limit > 0 && max_size > texture_limit) {
527                 pixels_storage.resize(((size_t)width)*height*depth*4);
528                 pixels = &pixels_storage[0];
529         }
530         else {
531                 pixels = (StorageType*)tex_img.resize(width, height, depth);
532         }
533         bool cmyk = false;
534         const size_t num_pixels = ((size_t)width) * height * depth;
535         if(in) {
536                 StorageType *readpixels = pixels;
537                 vector<StorageType> tmppixels;
538                 if(components > 4) {
539                         tmppixels.resize(((size_t)width)*height*components);
540                         readpixels = &tmppixels[0];
541                 }
542                 if(depth <= 1) {
543                         size_t scanlinesize = ((size_t)width)*components*sizeof(StorageType);
544                         in->read_image(FileFormat,
545                                        (uchar*)readpixels + (height-1)*scanlinesize,
546                                        AutoStride,
547                                        -scanlinesize,
548                                        AutoStride);
549                 }
550                 else {
551                         in->read_image(FileFormat, (uchar*)readpixels);
552                 }
553                 if(components > 4) {
554                         size_t dimensions = ((size_t)width)*height;
555                         for(size_t i = dimensions-1, pixel = 0; pixel < dimensions; pixel++, i--) {
556                                 pixels[i*4+3] = tmppixels[i*components+3];
557                                 pixels[i*4+2] = tmppixels[i*components+2];
558                                 pixels[i*4+1] = tmppixels[i*components+1];
559                                 pixels[i*4+0] = tmppixels[i*components+0];
560                         }
561                         tmppixels.clear();
562                 }
563                 cmyk = strcmp(in->format_name(), "jpeg") == 0 && components == 4;
564                 in->close();
565                 delete in;
566         }
567         else {
568                 if(FileFormat == TypeDesc::FLOAT) {
569                         builtin_image_float_pixels_cb(img->filename,
570                                                       img->builtin_data,
571                                                       (float*)&pixels[0],
572                                                       num_pixels * components);
573                 }
574                 else if(FileFormat == TypeDesc::UINT8) {
575                         builtin_image_pixels_cb(img->filename,
576                                                 img->builtin_data,
577                                                 (uchar*)&pixels[0],
578                                                 num_pixels * components);
579                 }
580                 else {
581                         /* TODO(dingto): Support half for ImBuf. */
582                 }
583         }
584         /* Check if we actually have a float4 slot, in case components == 1,
585          * but device doesn't support single channel textures.
586          */
587         bool is_rgba = (type == IMAGE_DATA_TYPE_FLOAT4 ||
588                         type == IMAGE_DATA_TYPE_HALF4 ||
589                         type == IMAGE_DATA_TYPE_BYTE4);
590         if(is_rgba) {
591                 if(cmyk) {
592                         /* CMYK */
593                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
594                                 pixels[i*4+2] = (pixels[i*4+2]*pixels[i*4+3])/255;
595                                 pixels[i*4+1] = (pixels[i*4+1]*pixels[i*4+3])/255;
596                                 pixels[i*4+0] = (pixels[i*4+0]*pixels[i*4+3])/255;
597                                 pixels[i*4+3] = alpha_one;
598                         }
599                 }
600                 else if(components == 2) {
601                         /* grayscale + alpha */
602                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
603                                 pixels[i*4+3] = pixels[i*2+1];
604                                 pixels[i*4+2] = pixels[i*2+0];
605                                 pixels[i*4+1] = pixels[i*2+0];
606                                 pixels[i*4+0] = pixels[i*2+0];
607                         }
608                 }
609                 else if(components == 3) {
610                         /* RGB */
611                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
612                                 pixels[i*4+3] = alpha_one;
613                                 pixels[i*4+2] = pixels[i*3+2];
614                                 pixels[i*4+1] = pixels[i*3+1];
615                                 pixels[i*4+0] = pixels[i*3+0];
616                         }
617                 }
618                 else if(components == 1) {
619                         /* grayscale */
620                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
621                                 pixels[i*4+3] = alpha_one;
622                                 pixels[i*4+2] = pixels[i];
623                                 pixels[i*4+1] = pixels[i];
624                                 pixels[i*4+0] = pixels[i];
625                         }
626                 }
627                 if(img->use_alpha == false) {
628                         for(size_t i = num_pixels-1, pixel = 0; pixel < num_pixels; pixel++, i--) {
629                                 pixels[i*4+3] = alpha_one;
630                         }
631                 }
632         }
633         if(pixels_storage.size() > 0) {
634                 float scale_factor = 1.0f;
635                 while(max_size * scale_factor > texture_limit) {
636                         scale_factor *= 0.5f;
637                 }
638                 VLOG(1) << "Scaling image " << img->filename
639                         << " by a factor of " << scale_factor << ".";
640                 vector<StorageType> scaled_pixels;
641                 size_t scaled_width, scaled_height, scaled_depth;
642                 util_image_resize_pixels(pixels_storage,
643                                          width, height, depth,
644                                          is_rgba ? 4 : 1,
645                                          scale_factor,
646                                          &scaled_pixels,
647                                          &scaled_width, &scaled_height, &scaled_depth);
648                 StorageType *texture_pixels = (StorageType*)tex_img.resize(scaled_width,
649                                                                            scaled_height,
650                                                                            scaled_depth);
651                 memcpy(texture_pixels,
652                        &scaled_pixels[0],
653                        scaled_pixels.size() * sizeof(StorageType));
654         }
655         return true;
656 }
657
658 void ImageManager::device_load_image(Device *device,
659                                      DeviceScene *dscene,
660                                      Scene *scene,
661                                      ImageDataType type,
662                                      int slot,
663                                      Progress *progress)
664 {
665         if(progress->get_cancel())
666                 return;
667
668         Image *img = images[type][slot];
669
670         if(osl_texture_system && !img->builtin_data)
671                 return;
672
673         string filename = path_filename(images[type][slot]->filename);
674         progress->set_status("Updating Images", "Loading " + filename);
675
676         const int texture_limit = scene->params.texture_limit;
677
678         /* Slot assignment */
679         int flat_slot = type_index_to_flattened_slot(slot, type);
680
681         string name = string_printf("__tex_image_%s_%03d", name_from_type(type).c_str(), flat_slot);
682
683         if(type == IMAGE_DATA_TYPE_FLOAT4) {
684                 if(dscene->tex_float4_image[slot] == NULL)
685                         dscene->tex_float4_image[slot] = new device_vector<float4>();
686                 device_vector<float4>& tex_img = *dscene->tex_float4_image[slot];
687
688                 if(tex_img.device_pointer) {
689                         thread_scoped_lock device_lock(device_mutex);
690                         device->tex_free(tex_img);
691                 }
692
693                 if(!file_load_image<TypeDesc::FLOAT, float>(img,
694                                                             type,
695                                                             texture_limit,
696                                                             tex_img))
697                 {
698                         /* on failure to load, we set a 1x1 pixels pink image */
699                         float *pixels = (float*)tex_img.resize(1, 1);
700
701                         pixels[0] = TEX_IMAGE_MISSING_R;
702                         pixels[1] = TEX_IMAGE_MISSING_G;
703                         pixels[2] = TEX_IMAGE_MISSING_B;
704                         pixels[3] = TEX_IMAGE_MISSING_A;
705                 }
706
707                 if(!pack_images) {
708                         thread_scoped_lock device_lock(device_mutex);
709                         device->tex_alloc(name.c_str(),
710                                           tex_img,
711                                           img->interpolation,
712                                           img->extension);
713                 }
714         }
715         else if(type == IMAGE_DATA_TYPE_FLOAT) {
716                 if(dscene->tex_float_image[slot] == NULL)
717                         dscene->tex_float_image[slot] = new device_vector<float>();
718                 device_vector<float>& tex_img = *dscene->tex_float_image[slot];
719
720                 if(tex_img.device_pointer) {
721                         thread_scoped_lock device_lock(device_mutex);
722                         device->tex_free(tex_img);
723                 }
724
725                 if(!file_load_image<TypeDesc::FLOAT, float>(img,
726                                                             type,
727                                                             texture_limit,
728                                                             tex_img))
729                 {
730                         /* on failure to load, we set a 1x1 pixels pink image */
731                         float *pixels = (float*)tex_img.resize(1, 1);
732
733                         pixels[0] = TEX_IMAGE_MISSING_R;
734                 }
735
736                 if(!pack_images) {
737                         thread_scoped_lock device_lock(device_mutex);
738                         device->tex_alloc(name.c_str(),
739                                           tex_img,
740                                           img->interpolation,
741                                           img->extension);
742                 }
743         }
744         else if(type == IMAGE_DATA_TYPE_BYTE4) {
745                 if(dscene->tex_byte4_image[slot] == NULL)
746                         dscene->tex_byte4_image[slot] = new device_vector<uchar4>();
747                 device_vector<uchar4>& tex_img = *dscene->tex_byte4_image[slot];
748
749                 if(tex_img.device_pointer) {
750                         thread_scoped_lock device_lock(device_mutex);
751                         device->tex_free(tex_img);
752                 }
753
754                 if(!file_load_image<TypeDesc::UINT8, uchar>(img,
755                                                             type,
756                                                             texture_limit,
757                                                             tex_img))
758                 {
759                         /* on failure to load, we set a 1x1 pixels pink image */
760                         uchar *pixels = (uchar*)tex_img.resize(1, 1);
761
762                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
763                         pixels[1] = (TEX_IMAGE_MISSING_G * 255);
764                         pixels[2] = (TEX_IMAGE_MISSING_B * 255);
765                         pixels[3] = (TEX_IMAGE_MISSING_A * 255);
766                 }
767
768                 if(!pack_images) {
769                         thread_scoped_lock device_lock(device_mutex);
770                         device->tex_alloc(name.c_str(),
771                                           tex_img,
772                                           img->interpolation,
773                                           img->extension);
774                 }
775         }
776         else if(type == IMAGE_DATA_TYPE_BYTE){
777                 if(dscene->tex_byte_image[slot] == NULL)
778                         dscene->tex_byte_image[slot] = new device_vector<uchar>();
779                 device_vector<uchar>& tex_img = *dscene->tex_byte_image[slot];
780
781                 if(tex_img.device_pointer) {
782                         thread_scoped_lock device_lock(device_mutex);
783                         device->tex_free(tex_img);
784                 }
785
786                 if(!file_load_image<TypeDesc::UINT8, uchar>(img,
787                                                             type,
788                                                             texture_limit,
789                                                             tex_img)) {
790                         /* on failure to load, we set a 1x1 pixels pink image */
791                         uchar *pixels = (uchar*)tex_img.resize(1, 1);
792
793                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
794                 }
795
796                 if(!pack_images) {
797                         thread_scoped_lock device_lock(device_mutex);
798                         device->tex_alloc(name.c_str(),
799                                           tex_img,
800                                           img->interpolation,
801                                           img->extension);
802                 }
803         }
804         else if(type == IMAGE_DATA_TYPE_HALF4){
805                 if(dscene->tex_half4_image[slot] == NULL)
806                         dscene->tex_half4_image[slot] = new device_vector<half4>();
807                 device_vector<half4>& tex_img = *dscene->tex_half4_image[slot];
808
809                 if(tex_img.device_pointer) {
810                         thread_scoped_lock device_lock(device_mutex);
811                         device->tex_free(tex_img);
812                 }
813
814                 if(!file_load_image<TypeDesc::HALF, half>(img,
815                                                           type,
816                                                           texture_limit,
817                                                           tex_img)) {
818                         /* on failure to load, we set a 1x1 pixels pink image */
819                         half *pixels = (half*)tex_img.resize(1, 1);
820
821                         pixels[0] = TEX_IMAGE_MISSING_R;
822                         pixels[1] = TEX_IMAGE_MISSING_G;
823                         pixels[2] = TEX_IMAGE_MISSING_B;
824                         pixels[3] = TEX_IMAGE_MISSING_A;
825                 }
826
827                 if(!pack_images) {
828                         thread_scoped_lock device_lock(device_mutex);
829                         device->tex_alloc(name.c_str(),
830                                           tex_img,
831                                           img->interpolation,
832                                           img->extension);
833                 }
834         }
835         else if(type == IMAGE_DATA_TYPE_HALF){
836                 if(dscene->tex_half_image[slot] == NULL)
837                         dscene->tex_half_image[slot] = new device_vector<half>();
838                 device_vector<half>& tex_img = *dscene->tex_half_image[slot];
839
840                 if(tex_img.device_pointer) {
841                         thread_scoped_lock device_lock(device_mutex);
842                         device->tex_free(tex_img);
843                 }
844
845                 if(!file_load_image<TypeDesc::HALF, half>(img,
846                                                           type,
847                                                           texture_limit,
848                                                           tex_img)) {
849                         /* on failure to load, we set a 1x1 pixels pink image */
850                         half *pixels = (half*)tex_img.resize(1, 1);
851
852                         pixels[0] = TEX_IMAGE_MISSING_R;
853                 }
854
855                 if(!pack_images) {
856                         thread_scoped_lock device_lock(device_mutex);
857                         device->tex_alloc(name.c_str(),
858                                           tex_img,
859                                           img->interpolation,
860                                           img->extension);
861                 }
862         }
863
864         img->need_load = false;
865 }
866
867 void ImageManager::device_free_image(Device *device, DeviceScene *dscene, ImageDataType type, int slot)
868 {
869         Image *img = images[type][slot];
870
871         if(img) {
872                 if(osl_texture_system && !img->builtin_data) {
873 #ifdef WITH_OSL
874                         ustring filename(images[type][slot]->filename);
875                         ((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
876 #endif
877                 }
878                 else {
879                         device_memory *tex_img = NULL;
880                         switch(type) {
881                                 case IMAGE_DATA_TYPE_FLOAT4:
882                                         tex_img = dscene->tex_float4_image[slot];
883                                         dscene->tex_float4_image[slot] = NULL;
884                                         break;
885                                 case IMAGE_DATA_TYPE_BYTE4:
886                                         tex_img = dscene->tex_byte4_image[slot];
887                                         dscene->tex_byte4_image[slot]= NULL;
888                                         break;
889                                 case IMAGE_DATA_TYPE_HALF4:
890                                         tex_img = dscene->tex_half4_image[slot];
891                                         dscene->tex_half4_image[slot]= NULL;
892                                         break;
893                                 case IMAGE_DATA_TYPE_FLOAT:
894                                         tex_img = dscene->tex_float_image[slot];
895                                         dscene->tex_float_image[slot] = NULL;
896                                         break;
897                                 case IMAGE_DATA_TYPE_BYTE:
898                                         tex_img = dscene->tex_byte_image[slot];
899                                         dscene->tex_byte_image[slot]= NULL;
900                                         break;
901                                 case IMAGE_DATA_TYPE_HALF:
902                                         tex_img = dscene->tex_half_image[slot];
903                                         dscene->tex_half_image[slot]= NULL;
904                                         break;
905                                 default:
906                                         assert(0);
907                                         tex_img = NULL;
908                         }
909                         if(tex_img) {
910                                 if(tex_img->device_pointer) {
911                                         thread_scoped_lock device_lock(device_mutex);
912                                         device->tex_free(*tex_img);
913                                 }
914
915                                 delete tex_img;
916                         }
917                 }
918
919                 delete images[type][slot];
920                 images[type][slot] = NULL;
921                 --tex_num_images[type];
922         }
923 }
924
925 void ImageManager::device_update(Device *device,
926                                  DeviceScene *dscene,
927                                  Scene *scene,
928                                  Progress& progress)
929 {
930         if(!need_update)
931                 return;
932
933         TaskPool pool;
934
935         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
936                 switch(type) {
937                         case IMAGE_DATA_TYPE_FLOAT4:
938                                 if(dscene->tex_float4_image.size() <= tex_num_images[IMAGE_DATA_TYPE_FLOAT4])
939                                         dscene->tex_float4_image.resize(tex_num_images[IMAGE_DATA_TYPE_FLOAT4]);
940                                 break;
941                         case IMAGE_DATA_TYPE_BYTE4:
942                                 if(dscene->tex_byte4_image.size() <= tex_num_images[IMAGE_DATA_TYPE_BYTE4])
943                                         dscene->tex_byte4_image.resize(tex_num_images[IMAGE_DATA_TYPE_BYTE4]);
944                                 break;
945                         case IMAGE_DATA_TYPE_HALF4:
946                                 if(dscene->tex_half4_image.size() <= tex_num_images[IMAGE_DATA_TYPE_HALF4])
947                                         dscene->tex_half4_image.resize(tex_num_images[IMAGE_DATA_TYPE_HALF4]);
948                                 break;
949                         case IMAGE_DATA_TYPE_BYTE:
950                                 if(dscene->tex_byte_image.size() <= tex_num_images[IMAGE_DATA_TYPE_BYTE])
951                                         dscene->tex_byte_image.resize(tex_num_images[IMAGE_DATA_TYPE_BYTE]);
952                                 break;
953                         case IMAGE_DATA_TYPE_FLOAT:
954                                 if(dscene->tex_float_image.size() <= tex_num_images[IMAGE_DATA_TYPE_FLOAT])
955                                         dscene->tex_float_image.resize(tex_num_images[IMAGE_DATA_TYPE_FLOAT]);
956                                 break;
957                         case IMAGE_DATA_TYPE_HALF:
958                                 if(dscene->tex_half_image.size() <= tex_num_images[IMAGE_DATA_TYPE_HALF])
959                                         dscene->tex_half_image.resize(tex_num_images[IMAGE_DATA_TYPE_HALF]);
960                                 break;
961                 }
962                 for(size_t slot = 0; slot < images[type].size(); slot++) {
963                         if(!images[type][slot])
964                                 continue;
965
966                         if(images[type][slot]->users == 0) {
967                                 device_free_image(device, dscene, (ImageDataType)type, slot);
968                         }
969                         else if(images[type][slot]->need_load) {
970                                 if(!osl_texture_system || images[type][slot]->builtin_data)
971                                         pool.push(function_bind(&ImageManager::device_load_image,
972                                                                 this,
973                                                                 device,
974                                                                 dscene,
975                                                                 scene,
976                                                                 (ImageDataType)type,
977                                                                 slot,
978                                                                 &progress));
979                         }
980                 }
981         }
982
983         pool.wait_work();
984
985         if(pack_images)
986                 device_pack_images(device, dscene, progress);
987
988         need_update = false;
989 }
990
991 void ImageManager::device_update_slot(Device *device,
992                                       DeviceScene *dscene,
993                                       Scene *scene,
994                                       int flat_slot,
995                                       Progress *progress)
996 {
997         ImageDataType type;
998         int slot = flattened_slot_to_type_index(flat_slot, &type);
999
1000         Image *image = images[type][slot];
1001         assert(image != NULL);
1002
1003         if(image->users == 0) {
1004                 device_free_image(device, dscene, type, slot);
1005         }
1006         else if(image->need_load) {
1007                 if(!osl_texture_system || image->builtin_data)
1008                         device_load_image(device,
1009                                           dscene,
1010                                           scene,
1011                                           type,
1012                                           slot,
1013                                           progress);
1014         }
1015 }
1016
1017 uint8_t ImageManager::pack_image_options(ImageDataType type, size_t slot)
1018 {
1019         uint8_t options = 0;
1020
1021         /* Image Options are packed into one uint:
1022          * bit 0 -> Interpolation
1023          * bit 1 + 2  + 3-> Extension */
1024         if(images[type][slot]->interpolation == INTERPOLATION_CLOSEST)
1025                 options |= (1 << 0);
1026
1027         if(images[type][slot]->extension == EXTENSION_REPEAT)
1028                 options |= (1 << 1);
1029         else if(images[type][slot]->extension == EXTENSION_EXTEND)
1030                 options |= (1 << 2);
1031         else /* EXTENSION_CLIP */
1032                 options |= (1 << 3);
1033
1034         return options;
1035 }
1036
1037 void ImageManager::device_pack_images(Device *device,
1038                                       DeviceScene *dscene,
1039                                       Progress& /*progess*/)
1040 {
1041         /* For OpenCL, we pack all image textures into a single large texture, and
1042          * do our own interpolation in the kernel. */
1043         size_t size = 0, offset = 0;
1044         ImageDataType type;
1045
1046         int info_size = tex_num_images[IMAGE_DATA_TYPE_FLOAT4] + tex_num_images[IMAGE_DATA_TYPE_BYTE4]
1047                         + tex_num_images[IMAGE_DATA_TYPE_FLOAT] + tex_num_images[IMAGE_DATA_TYPE_BYTE];
1048         uint4 *info = dscene->tex_image_packed_info.resize(info_size*2);
1049
1050         /* Byte4 Textures*/
1051         type = IMAGE_DATA_TYPE_BYTE4;
1052
1053         for(size_t slot = 0; slot < images[type].size(); slot++) {
1054                 if(!images[type][slot])
1055                         continue;
1056
1057                 device_vector<uchar4>& tex_img = *dscene->tex_byte4_image[slot];
1058                 size += tex_img.size();
1059         }
1060
1061         uchar4 *pixels_byte4 = dscene->tex_image_byte4_packed.resize(size);
1062
1063         for(size_t slot = 0; slot < images[type].size(); slot++) {
1064                 if(!images[type][slot])
1065                         continue;
1066
1067                 device_vector<uchar4>& tex_img = *dscene->tex_byte4_image[slot];
1068
1069                 uint8_t options = pack_image_options(type, slot);
1070
1071                 int index = type_index_to_flattened_slot(slot, type) * 2;
1072                 info[index] = make_uint4(tex_img.data_width, tex_img.data_height, offset, options);
1073                 info[index+1] = make_uint4(tex_img.data_depth, 0, 0, 0);
1074
1075                 memcpy(pixels_byte4+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
1076                 offset += tex_img.size();
1077         }
1078
1079         /* Float4 Textures*/
1080         type = IMAGE_DATA_TYPE_FLOAT4;
1081         size = 0, offset = 0;
1082
1083         for(size_t slot = 0; slot < images[type].size(); slot++) {
1084                 if(!images[type][slot])
1085                         continue;
1086
1087                 device_vector<float4>& tex_img = *dscene->tex_float4_image[slot];
1088                 size += tex_img.size();
1089         }
1090
1091         float4 *pixels_float4 = dscene->tex_image_float4_packed.resize(size);
1092
1093         for(size_t slot = 0; slot < images[type].size(); slot++) {
1094                 if(!images[type][slot])
1095                         continue;
1096
1097                 device_vector<float4>& tex_img = *dscene->tex_float4_image[slot];
1098
1099                 /* todo: support 3D textures, only CPU for now */
1100
1101                 uint8_t options = pack_image_options(type, slot);
1102
1103                 int index = type_index_to_flattened_slot(slot, type) * 2;
1104                 info[index] = make_uint4(tex_img.data_width, tex_img.data_height, offset, options);
1105                 info[index+1] = make_uint4(tex_img.data_depth, 0, 0, 0);
1106
1107                 memcpy(pixels_float4+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
1108                 offset += tex_img.size();
1109         }
1110
1111         /* Byte Textures*/
1112         type = IMAGE_DATA_TYPE_BYTE;
1113         size = 0, offset = 0;
1114
1115         for(size_t slot = 0; slot < images[type].size(); slot++) {
1116                 if(!images[type][slot])
1117                         continue;
1118
1119                 device_vector<uchar>& tex_img = *dscene->tex_byte_image[slot];
1120                 size += tex_img.size();
1121         }
1122
1123         uchar *pixels_byte = dscene->tex_image_byte_packed.resize(size);
1124
1125         for(size_t slot = 0; slot < images[type].size(); slot++) {
1126                 if(!images[type][slot])
1127                         continue;
1128
1129                 device_vector<uchar>& tex_img = *dscene->tex_byte_image[slot];
1130
1131                 uint8_t options = pack_image_options(type, slot);
1132
1133                 int index = type_index_to_flattened_slot(slot, type) * 2;
1134                 info[index] = make_uint4(tex_img.data_width, tex_img.data_height, offset, options);
1135                 info[index+1] = make_uint4(tex_img.data_depth, 0, 0, 0);
1136
1137                 memcpy(pixels_byte+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
1138                 offset += tex_img.size();
1139         }
1140
1141         /* Float Textures*/
1142         type = IMAGE_DATA_TYPE_FLOAT;
1143         size = 0, offset = 0;
1144
1145         for(size_t slot = 0; slot < images[type].size(); slot++) {
1146                 if(!images[type][slot])
1147                         continue;
1148
1149                 device_vector<float>& tex_img = *dscene->tex_float_image[slot];
1150                 size += tex_img.size();
1151         }
1152
1153         float *pixels_float = dscene->tex_image_float_packed.resize(size);
1154
1155         for(size_t slot = 0; slot < images[type].size(); slot++) {
1156                 if(!images[type][slot])
1157                         continue;
1158
1159                 device_vector<float>& tex_img = *dscene->tex_float_image[slot];
1160
1161                 /* todo: support 3D textures, only CPU for now */
1162
1163                 uint8_t options = pack_image_options(type, slot);
1164
1165                 int index = type_index_to_flattened_slot(slot, type) * 2;
1166                 info[index] = make_uint4(tex_img.data_width, tex_img.data_height, offset, options);
1167                 info[index+1] = make_uint4(tex_img.data_depth, 0, 0, 0);
1168
1169                 memcpy(pixels_float+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
1170                 offset += tex_img.size();
1171         }
1172
1173         if(dscene->tex_image_byte4_packed.size()) {
1174                 if(dscene->tex_image_byte4_packed.device_pointer) {
1175                         thread_scoped_lock device_lock(device_mutex);
1176                         device->tex_free(dscene->tex_image_byte4_packed);
1177                 }
1178                 device->tex_alloc("__tex_image_byte4_packed", dscene->tex_image_byte4_packed);
1179         }
1180         if(dscene->tex_image_float4_packed.size()) {
1181                 if(dscene->tex_image_float4_packed.device_pointer) {
1182                         thread_scoped_lock device_lock(device_mutex);
1183                         device->tex_free(dscene->tex_image_float4_packed);
1184                 }
1185                 device->tex_alloc("__tex_image_float4_packed", dscene->tex_image_float4_packed);
1186         }
1187         if(dscene->tex_image_byte_packed.size()) {
1188                 if(dscene->tex_image_byte_packed.device_pointer) {
1189                         thread_scoped_lock device_lock(device_mutex);
1190                         device->tex_free(dscene->tex_image_byte_packed);
1191                 }
1192                 device->tex_alloc("__tex_image_byte_packed", dscene->tex_image_byte_packed);
1193         }
1194         if(dscene->tex_image_float_packed.size()) {
1195                 if(dscene->tex_image_float_packed.device_pointer) {
1196                         thread_scoped_lock device_lock(device_mutex);
1197                         device->tex_free(dscene->tex_image_float_packed);
1198                 }
1199                 device->tex_alloc("__tex_image_float_packed", dscene->tex_image_float_packed);
1200         }
1201         if(dscene->tex_image_packed_info.size()) {
1202                 if(dscene->tex_image_packed_info.device_pointer) {
1203                         thread_scoped_lock device_lock(device_mutex);
1204                         device->tex_free(dscene->tex_image_packed_info);
1205                 }
1206                 device->tex_alloc("__tex_image_packed_info", dscene->tex_image_packed_info);
1207         }
1208 }
1209
1210 void ImageManager::device_free_builtin(Device *device, DeviceScene *dscene)
1211 {
1212         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1213                 for(size_t slot = 0; slot < images[type].size(); slot++) {
1214                         if(images[type][slot] && images[type][slot]->builtin_data)
1215                                 device_free_image(device, dscene, (ImageDataType)type, slot);
1216                 }
1217         }
1218 }
1219
1220 void ImageManager::device_free(Device *device, DeviceScene *dscene)
1221 {
1222         for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
1223                 for(size_t slot = 0; slot < images[type].size(); slot++) {
1224                         device_free_image(device, dscene, (ImageDataType)type, slot);
1225                 }
1226                 images[type].clear();
1227         }
1228
1229         dscene->tex_float4_image.clear();
1230         dscene->tex_byte4_image.clear();
1231         dscene->tex_half4_image.clear();
1232         dscene->tex_float_image.clear();
1233         dscene->tex_byte_image.clear();
1234         dscene->tex_half_image.clear();
1235
1236         device->tex_free(dscene->tex_image_float4_packed);
1237         device->tex_free(dscene->tex_image_byte4_packed);
1238         device->tex_free(dscene->tex_image_float_packed);
1239         device->tex_free(dscene->tex_image_byte_packed);
1240         device->tex_free(dscene->tex_image_packed_info);
1241
1242         dscene->tex_image_float4_packed.clear();
1243         dscene->tex_image_byte4_packed.clear();
1244         dscene->tex_image_float_packed.clear();
1245         dscene->tex_image_byte_packed.clear();
1246         dscene->tex_image_packed_info.clear();
1247 }
1248
1249 CCL_NAMESPACE_END
1250