Merged revision(s) 59035-59107 from trunk/blender into soc-2013-dingto.
[blender.git] / intern / cycles / render / image.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20 #include "image.h"
21 #include "scene.h"
22
23 #include "util_foreach.h"
24 #include "util_image.h"
25 #include "util_path.h"
26 #include "util_progress.h"
27
28 #ifdef WITH_OSL
29 #include <OSL/oslexec.h>
30 #endif
31
32 CCL_NAMESPACE_BEGIN
33
34 ImageManager::ImageManager()
35 {
36         need_update = true;
37         pack_images = false;
38         osl_texture_system = NULL;
39         animation_frame = 0;
40
41         tex_num_images = TEX_NUM_IMAGES;
42         tex_num_float_images = TEX_NUM_FLOAT_IMAGES;
43         tex_image_byte_start = TEX_IMAGE_BYTE_START;
44 }
45
46 ImageManager::~ImageManager()
47 {
48         for(size_t slot = 0; slot < images.size(); slot++)
49                 assert(!images[slot]);
50         for(size_t slot = 0; slot < float_images.size(); slot++)
51                 assert(!float_images[slot]);
52 }
53
54 void ImageManager::set_pack_images(bool pack_images_)
55 {
56         pack_images = pack_images_;
57 }
58
59 void ImageManager::set_osl_texture_system(void *texture_system)
60 {
61         osl_texture_system = texture_system;
62 }
63
64 void ImageManager::set_extended_image_limits(const DeviceInfo& info)
65 {
66         if(info.type == DEVICE_CPU) {
67                 tex_num_images = TEX_EXTENDED_NUM_IMAGES_CPU;
68                 tex_num_float_images = TEX_EXTENDED_NUM_FLOAT_IMAGES;
69                 tex_image_byte_start = TEX_EXTENDED_IMAGE_BYTE_START;
70         }
71         else if ((info.type == DEVICE_CUDA || info.type == DEVICE_MULTI) && info.extended_images) {
72                 tex_num_images = TEX_EXTENDED_NUM_IMAGES_GPU;
73         }
74 }
75
76 bool ImageManager::set_animation_frame_update(int frame)
77 {
78         if(frame != animation_frame) {
79                 animation_frame = frame;
80
81                 for(size_t slot = 0; slot < images.size(); slot++)
82                         if(images[slot] && images[slot]->animated)
83                                 return true;
84
85                 for(size_t slot = 0; slot < float_images.size(); slot++)
86                         if(float_images[slot] && float_images[slot]->animated)
87                                 return true;
88         }
89         
90         return false;
91 }
92
93 bool ImageManager::is_float_image(const string& filename, void *builtin_data, bool& is_linear)
94 {
95         bool is_float = false;
96         is_linear = false;
97
98         if(builtin_data) {
99                 if(builtin_image_info_cb) {
100                         int width, height, channels;
101                         builtin_image_info_cb(filename, builtin_data, is_float, width, height, channels);
102                 }
103
104                 if(is_float)
105                         is_linear = true;
106
107                 return is_float;
108         }
109
110         ImageInput *in = ImageInput::create(filename);
111
112         if(in) {
113                 ImageSpec spec;
114
115                 if(in->open(filename, spec)) {
116                         /* check the main format, and channel formats;
117                          * if any take up more than one byte, we'll need a float texture slot */
118                         if(spec.format.basesize() > 1) {
119                                 is_float = true;
120                                 is_linear = true;
121                         }
122
123                         for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
124                                 if(spec.channelformats[channel].basesize() > 1) {
125                                         is_float = true;
126                                         is_linear = true;
127                                 }
128                         }
129
130                         /* basic color space detection, not great but better than nothing
131                          * before we do OpenColorIO integration */
132                         if(is_float) {
133                                 string colorspace = spec.get_string_attribute("oiio:ColorSpace");
134
135                                 is_linear = !(colorspace == "sRGB" ||
136                                               colorspace == "GammaCorrected" ||
137                                                           strcmp(in->format_name(), "png") == 0);
138                         }
139                         else
140                                 is_linear = false;
141
142                         in->close();
143                 }
144
145                 delete in;
146         }
147
148         return is_float;
149 }
150
151 int ImageManager::add_image(const string& filename, void *builtin_data, bool animated, bool& is_float, bool& is_linear)
152 {
153         Image *img;
154         size_t slot;
155
156         /* load image info and find out if we need a float texture */
157         is_float = (pack_images)? false: is_float_image(filename, builtin_data, is_linear);
158
159         if(is_float) {
160                 /* find existing image */
161                 for(slot = 0; slot < float_images.size(); slot++) {
162                         if(float_images[slot] && float_images[slot]->filename == filename) {
163                                 float_images[slot]->users++;
164                                 return slot;
165                         }
166                 }
167
168                 /* find free slot */
169                 for(slot = 0; slot < float_images.size(); slot++) {
170                         if(!float_images[slot])
171                                 break;
172                 }
173
174                 if(slot == float_images.size()) {
175                         /* max images limit reached */
176                         if(float_images.size() == TEX_NUM_FLOAT_IMAGES) {
177                                 printf("ImageManager::add_image: float image limit reached %d, skipping '%s'\n",
178                                        tex_num_float_images, filename.c_str());
179                                 return -1;
180                         }
181
182                         float_images.resize(float_images.size() + 1);
183                 }
184
185                 /* add new image */
186                 img = new Image();
187                 img->filename = filename;
188                 img->builtin_data = builtin_data;
189                 img->need_load = true;
190                 img->animated = animated;
191                 img->users = 1;
192
193                 float_images[slot] = img;
194         }
195         else {
196                 for(slot = 0; slot < images.size(); slot++) {
197                         if(images[slot] && images[slot]->filename == filename) {
198                                 images[slot]->users++;
199                                 return slot+tex_image_byte_start;
200                         }
201                 }
202
203                 /* find free slot */
204                 for(slot = 0; slot < images.size(); slot++) {
205                         if(!images[slot])
206                                 break;
207                 }
208
209                 if(slot == images.size()) {
210                         /* max images limit reached */
211                         if(images.size() == tex_num_images) {
212                                 printf("ImageManager::add_image: byte image limit reached %d, skipping '%s'\n",
213                                        tex_num_images, filename.c_str());
214                                 return -1;
215                         }
216
217                         images.resize(images.size() + 1);
218                 }
219
220                 /* add new image */
221                 img = new Image();
222                 img->filename = filename;
223                 img->builtin_data = builtin_data;
224                 img->need_load = true;
225                 img->animated = animated;
226                 img->users = 1;
227
228                 images[slot] = img;
229
230                 slot += tex_image_byte_start;
231         }
232         need_update = true;
233
234         return slot;
235 }
236
237 void ImageManager::remove_image(const string& filename, void *builtin_data)
238 {
239         size_t slot;
240
241         for(slot = 0; slot < images.size(); slot++) {
242                 if(images[slot] && images[slot]->filename == filename && images[slot]->builtin_data == builtin_data) {
243                         /* decrement user count */
244                         images[slot]->users--;
245                         assert(images[slot]->users >= 0);
246
247                         /* don't remove immediately, rather do it all together later on. one of
248                          * the reasons for this is that on shader changes we add and remove nodes
249                          * that use them, but we do not want to reload the image all the time. */
250                         if(images[slot]->users == 0)
251                                 need_update = true;
252
253                         break;
254                 }
255         }
256
257         if(slot == images.size()) {
258                 /* see if it's in a float texture slot */
259                 for(slot = 0; slot < float_images.size(); slot++) {
260                         if(float_images[slot] && float_images[slot]->filename == filename && float_images[slot]->builtin_data == builtin_data) {
261                                 /* decrement user count */
262                                 float_images[slot]->users--;
263                                 assert(float_images[slot]->users >= 0);
264
265                                 /* don't remove immediately, rather do it all together later on. one of
266                                  * the reasons for this is that on shader changes we add and remove nodes
267                                  * that use them, but we do not want to reload the image all the time. */
268                                 if(float_images[slot]->users == 0)
269                                         need_update = true;
270
271                                 break;
272                         }
273                 }
274         }
275 }
276
277 bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
278 {
279         if(img->filename == "")
280                 return false;
281
282         ImageInput *in = NULL;
283         int width, height, components;
284
285         if(!img->builtin_data) {
286                 /* load image from file through OIIO */
287                 in = ImageInput::create(img->filename);
288
289                 if(!in)
290                         return false;
291
292                 ImageSpec spec;
293
294                 if(!in->open(img->filename, spec)) {
295                         delete in;
296                         return false;
297                 }
298
299                 width = spec.width;
300                 height = spec.height;
301                 components = spec.nchannels;
302         }
303         else {
304                 /* load image using builtin images callbacks */
305                 if(!builtin_image_info_cb || !builtin_image_pixels_cb)
306                         return false;
307
308                 bool is_float;
309                 builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, components);
310         }
311
312         /* we only handle certain number of components */
313         if(!(components == 1 || components == 3 || components == 4)) {
314                 if(in) {
315                         in->close();
316                         delete in;
317                 }
318
319                 return false;
320         }
321
322         /* read RGBA pixels */
323         uchar *pixels = (uchar*)tex_img.resize(width, height);
324         int scanlinesize = width*components*sizeof(uchar);
325
326         if(in) {
327                 in->read_image(TypeDesc::UINT8,
328                         (uchar*)pixels + (height-1)*scanlinesize,
329                         AutoStride,
330                         -scanlinesize,
331                         AutoStride);
332
333                 in->close();
334                 delete in;
335         }
336         else {
337                 builtin_image_pixels_cb(img->filename, img->builtin_data, pixels);
338         }
339
340         if(components == 3) {
341                 for(int i = width*height-1; i >= 0; i--) {
342                         pixels[i*4+3] = 255;
343                         pixels[i*4+2] = pixels[i*3+2];
344                         pixels[i*4+1] = pixels[i*3+1];
345                         pixels[i*4+0] = pixels[i*3+0];
346                 }
347         }
348         else if(components == 1) {
349                 for(int i = width*height-1; i >= 0; i--) {
350                         pixels[i*4+3] = 255;
351                         pixels[i*4+2] = pixels[i];
352                         pixels[i*4+1] = pixels[i];
353                         pixels[i*4+0] = pixels[i];
354                 }
355         }
356
357         return true;
358 }
359
360 bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_img)
361 {
362         if(img->filename == "")
363                 return false;
364
365         ImageInput *in = NULL;
366         int width, height, components;
367
368         if(!img->builtin_data) {
369                 /* load image from file through OIIO */
370                 in = ImageInput::create(img->filename);
371
372                 if(!in)
373                         return false;
374
375                 ImageSpec spec;
376
377                 if(!in->open(img->filename, spec)) {
378                         delete in;
379                         return false;
380                 }
381
382                 /* we only handle certain number of components */
383                 width = spec.width;
384                 height = spec.height;
385                 components = spec.nchannels;
386         }
387         else {
388                 /* load image using builtin images callbacks */
389                 if(!builtin_image_info_cb || !builtin_image_float_pixels_cb)
390                         return false;
391
392                 bool is_float;
393                 builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, components);
394         }
395
396         if(!(components == 1 || components == 3 || components == 4)) {
397                 if(in) {
398                         in->close();
399                         delete in;
400                 }
401                 return false;
402         }
403
404         /* read RGBA pixels */
405         float *pixels = (float*)tex_img.resize(width, height);
406         int scanlinesize = width*components*sizeof(float);
407
408         if(in) {
409                 in->read_image(TypeDesc::FLOAT,
410                         (uchar*)pixels + (height-1)*scanlinesize,
411                         AutoStride,
412                         -scanlinesize,
413                         AutoStride);
414
415                 in->close();
416                 delete in;
417         }
418         else {
419                 builtin_image_float_pixels_cb(img->filename, img->builtin_data, pixels);
420         }
421
422         if(components == 3) {
423                 for(int i = width*height-1; i >= 0; i--) {
424                         pixels[i*4+3] = 1.0f;
425                         pixels[i*4+2] = pixels[i*3+2];
426                         pixels[i*4+1] = pixels[i*3+1];
427                         pixels[i*4+0] = pixels[i*3+0];
428                 }
429         }
430         else if(components == 1) {
431                 for(int i = width*height-1; i >= 0; i--) {
432                         pixels[i*4+3] = 1.0f;
433                         pixels[i*4+2] = pixels[i];
434                         pixels[i*4+1] = pixels[i];
435                         pixels[i*4+0] = pixels[i];
436                 }
437         }
438
439         return true;
440 }
441
442 void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int slot, Progress *progress)
443 {
444         if(progress->get_cancel())
445                 return;
446         if(osl_texture_system)
447                 return;
448
449         Image *img;
450         bool is_float;
451
452         if(slot >= tex_image_byte_start) {
453                 img = images[slot - tex_image_byte_start];
454                 is_float = false;
455         }
456         else {
457                 img = float_images[slot];
458                 is_float = true;
459         }
460
461         if(is_float) {
462                 string filename = path_filename(float_images[slot]->filename);
463                 progress->set_status("Updating Images", "Loading " + filename);
464
465                 device_vector<float4>& tex_img = dscene->tex_float_image[slot];
466
467                 if(tex_img.device_pointer) {
468                         thread_scoped_lock device_lock(device_mutex);
469                         device->tex_free(tex_img);
470                 }
471
472                 if(!file_load_float_image(img, tex_img)) {
473                         /* on failure to load, we set a 1x1 pixels pink image */
474                         float *pixels = (float*)tex_img.resize(1, 1);
475
476                         pixels[0] = TEX_IMAGE_MISSING_R;
477                         pixels[1] = TEX_IMAGE_MISSING_G;
478                         pixels[2] = TEX_IMAGE_MISSING_B;
479                         pixels[3] = TEX_IMAGE_MISSING_A;
480                 }
481
482                 string name;
483
484                 if(slot >= 10) name = string_printf("__tex_image_float_0%d", slot);
485                 else name = string_printf("__tex_image_float_00%d", slot);
486
487                 if(!pack_images) {
488                         thread_scoped_lock device_lock(device_mutex);
489                         device->tex_alloc(name.c_str(), tex_img, true, true);
490                 }
491         }
492         else {
493                 string filename = path_filename(images[slot - tex_image_byte_start]->filename);
494                 progress->set_status("Updating Images", "Loading " + filename);
495
496                 device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
497
498                 if(tex_img.device_pointer) {
499                         thread_scoped_lock device_lock(device_mutex);
500                         device->tex_free(tex_img);
501                 }
502
503                 if(!file_load_image(img, tex_img)) {
504                         /* on failure to load, we set a 1x1 pixels pink image */
505                         uchar *pixels = (uchar*)tex_img.resize(1, 1);
506
507                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
508                         pixels[1] = (TEX_IMAGE_MISSING_G * 255);
509                         pixels[2] = (TEX_IMAGE_MISSING_B * 255);
510                         pixels[3] = (TEX_IMAGE_MISSING_A * 255);
511                 }
512
513                 string name;
514
515                 if(slot >= 10) name = string_printf("__tex_image_0%d", slot);
516                 else name = string_printf("__tex_image_00%d", slot);
517
518                 if(!pack_images) {
519                         thread_scoped_lock device_lock(device_mutex);
520                         device->tex_alloc(name.c_str(), tex_img, true, true);
521                 }
522         }
523
524         img->need_load = false;
525 }
526
527 void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int slot)
528 {
529         Image *img;
530         bool is_float;
531
532         if(slot >= tex_image_byte_start) {
533                 img = images[slot - tex_image_byte_start];
534                 is_float = false;
535         }
536         else {
537                 img = float_images[slot];
538                 is_float = true;
539         }
540
541         if(img) {
542                 if(osl_texture_system) {
543 #ifdef WITH_OSL
544                         ustring filename(images[slot]->filename);
545                         ((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
546 #endif
547                 }
548                 else if(is_float) {
549                         device_vector<float4>& tex_img = dscene->tex_float_image[slot];
550
551                         if(tex_img.device_pointer) {
552                                 thread_scoped_lock device_lock(device_mutex);
553                                 device->tex_free(tex_img);
554                         }
555
556                         tex_img.clear();
557
558                         delete float_images[slot];
559                         float_images[slot] = NULL;
560                 }
561                 else {
562                         device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
563
564                         if(tex_img.device_pointer) {
565                                 thread_scoped_lock device_lock(device_mutex);
566                                 device->tex_free(tex_img);
567                         }
568
569                         tex_img.clear();
570
571                         delete images[slot - tex_image_byte_start];
572                         images[slot - tex_image_byte_start] = NULL;
573                 }
574         }
575 }
576
577 void ImageManager::device_update(Device *device, DeviceScene *dscene, Progress& progress)
578 {
579         if(!need_update)
580                 return;
581
582         TaskPool pool;
583
584         for(size_t slot = 0; slot < images.size(); slot++) {
585                 if(!images[slot])
586                         continue;
587
588                 if(images[slot]->users == 0) {
589                         device_free_image(device, dscene, slot + tex_image_byte_start);
590                 }
591                 else if(images[slot]->need_load) {
592                         if(!osl_texture_system) 
593                                 pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot + tex_image_byte_start, &progress));
594                 }
595         }
596
597         for(size_t slot = 0; slot < float_images.size(); slot++) {
598                 if(!float_images[slot])
599                         continue;
600
601                 if(float_images[slot]->users == 0) {
602                         device_free_image(device, dscene, slot);
603                 }
604                 else if(float_images[slot]->need_load) {
605                         if(!osl_texture_system) 
606                                 pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot, &progress));
607                 }
608         }
609
610         pool.wait_work();
611
612         if(pack_images)
613                 device_pack_images(device, dscene, progress);
614
615         need_update = false;
616 }
617
618 void ImageManager::device_pack_images(Device *device, DeviceScene *dscene, Progress& progess)
619 {
620         /* for OpenCL, we pack all image textures inside a single big texture, and
621          * will do our own interpolation in the kernel */
622         size_t size = 0;
623
624         for(size_t slot = 0; slot < images.size(); slot++) {
625                 if(!images[slot])
626                         continue;
627
628                 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
629                 size += tex_img.size();
630         }
631
632         uint4 *info = dscene->tex_image_packed_info.resize(images.size());
633         uchar4 *pixels = dscene->tex_image_packed.resize(size);
634
635         size_t offset = 0;
636
637         for(size_t slot = 0; slot < images.size(); slot++) {
638                 if(!images[slot])
639                         continue;
640
641                 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
642
643                 info[slot] = make_uint4(tex_img.data_width, tex_img.data_height, offset, 1);
644
645                 memcpy(pixels+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
646                 offset += tex_img.size();
647         }
648
649         if(dscene->tex_image_packed.size())
650                 device->tex_alloc("__tex_image_packed", dscene->tex_image_packed);
651         if(dscene->tex_image_packed_info.size())
652                 device->tex_alloc("__tex_image_packed_info", dscene->tex_image_packed_info);
653 }
654
655 void ImageManager::device_free(Device *device, DeviceScene *dscene)
656 {
657         for(size_t slot = 0; slot < images.size(); slot++)
658                 device_free_image(device, dscene, slot + tex_image_byte_start);
659         for(size_t slot = 0; slot < float_images.size(); slot++)
660                 device_free_image(device, dscene, slot);
661
662         device->tex_free(dscene->tex_image_packed);
663         device->tex_free(dscene->tex_image_packed_info);
664
665         dscene->tex_image_packed.clear();
666         dscene->tex_image_packed_info.clear();
667
668         images.clear();
669         float_images.clear();
670 }
671
672 CCL_NAMESPACE_END
673