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