Fix #32796: cycles did not support image auto refresh option.
[blender-staging.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(void)
65 {
66         tex_num_images = TEX_EXTENDED_NUM_IMAGES;
67         tex_num_float_images = TEX_EXTENDED_NUM_FLOAT_IMAGES;
68         tex_image_byte_start = TEX_EXTENDED_IMAGE_BYTE_START;
69 }
70
71 bool ImageManager::set_animation_frame_update(int frame)
72 {
73         if(frame != animation_frame) {
74                 animation_frame = frame;
75
76                 for(size_t slot = 0; slot < images.size(); slot++)
77                         if(images[slot] && images[slot]->animated)
78                                 return true;
79
80                 for(size_t slot = 0; slot < float_images.size(); slot++)
81                         if(float_images[slot] && float_images[slot]->animated)
82                                 return true;
83         }
84         
85         return false;
86 }
87
88 bool ImageManager::is_float_image(const string& filename)
89 {
90         ImageInput *in = ImageInput::create(filename);
91         bool is_float = false;
92
93         if(in) {
94                 ImageSpec spec;
95
96                 if(in->open(filename, spec)) {
97                         /* check the main format, and channel formats;
98                          * if any take up more than one byte, we'll need a float texture slot */
99                         if(spec.format.basesize() > 1)
100                                 is_float = true;
101
102                         for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
103                                 if(spec.channelformats[channel].basesize() > 1)
104                                         is_float = true;
105                         }
106
107                         in->close();
108                 }
109
110                 delete in;
111         }
112
113         return is_float;
114 }
115
116 int ImageManager::add_image(const string& filename, bool animated, bool& is_float)
117 {
118         Image *img;
119         size_t slot;
120
121         /* load image info and find out if we need a float texture */
122         is_float = (pack_images)? false: is_float_image(filename);
123
124         if(is_float) {
125                 /* find existing image */
126                 for(slot = 0; slot < float_images.size(); slot++) {
127                         if(float_images[slot] && float_images[slot]->filename == filename) {
128                                 float_images[slot]->users++;
129                                 return slot;
130                         }
131                 }
132
133                 /* find free slot */
134                 for(slot = 0; slot < float_images.size(); slot++) {
135                         if(!float_images[slot])
136                                 break;
137                 }
138
139                 if(slot == float_images.size()) {
140                         /* max images limit reached */
141                         if(float_images.size() == TEX_NUM_FLOAT_IMAGES) {
142                                 printf("ImageManager::add_image: float image limit reached %d, skipping '%s'\n",
143                                        tex_num_float_images, filename.c_str());
144                                 return -1;
145                         }
146
147                         float_images.resize(float_images.size() + 1);
148                 }
149
150                 /* add new image */
151                 img = new Image();
152                 img->filename = filename;
153                 img->need_load = true;
154                 img->animated = animated;
155                 img->users = 1;
156
157                 float_images[slot] = img;
158         }
159         else {
160                 for(slot = 0; slot < images.size(); slot++) {
161                         if(images[slot] && images[slot]->filename == filename) {
162                                 images[slot]->users++;
163                                 return slot+tex_image_byte_start;
164                         }
165                 }
166
167                 /* find free slot */
168                 for(slot = 0; slot < images.size(); slot++) {
169                         if(!images[slot])
170                                 break;
171                 }
172
173                 if(slot == images.size()) {
174                         /* max images limit reached */
175                         if(images.size() == tex_num_images) {
176                                 printf("ImageManager::add_image: byte image limit reached %d, skipping '%s'\n",
177                                        tex_num_images, filename.c_str());
178                                 return -1;
179                         }
180
181                         images.resize(images.size() + 1);
182                 }
183
184                 /* add new image */
185                 img = new Image();
186                 img->filename = filename;
187                 img->need_load = true;
188                 img->animated = animated;
189                 img->users = 1;
190
191                 images[slot] = img;
192
193                 slot += tex_image_byte_start;
194         }
195         need_update = true;
196
197         return slot;
198 }
199
200 void ImageManager::remove_image(const string& filename)
201 {
202         size_t slot;
203
204         for(slot = 0; slot < images.size(); slot++) {
205                 if(images[slot] && images[slot]->filename == filename) {
206                         /* decrement user count */
207                         images[slot]->users--;
208                         assert(images[slot]->users >= 0);
209
210                         /* don't remove immediately, rather do it all together later on. one of
211                          * the reasons for this is that on shader changes we add and remove nodes
212                          * that use them, but we do not want to reload the image all the time. */
213                         if(images[slot]->users == 0)
214                                 need_update = true;
215
216                         break;
217                 }
218         }
219
220         if(slot == images.size()) {
221                 /* see if it's in a float texture slot */
222                 for(slot = 0; slot < float_images.size(); slot++) {
223                         if(float_images[slot] && float_images[slot]->filename == filename) {
224                                 /* decrement user count */
225                                 float_images[slot]->users--;
226                                 assert(float_images[slot]->users >= 0);
227
228                                 /* don't remove immediately, rather do it all together later on. one of
229                                  * the reasons for this is that on shader changes we add and remove nodes
230                                  * that use them, but we do not want to reload the image all the time. */
231                                 if(float_images[slot]->users == 0)
232                                         need_update = true;
233
234                                 break;
235                         }
236                 }
237         }
238 }
239
240 bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
241 {
242         if(img->filename == "")
243                 return false;
244
245         /* load image from file through OIIO */
246         ImageInput *in = ImageInput::create(img->filename);
247
248         if(!in)
249                 return false;
250
251         ImageSpec spec;
252
253         if(!in->open(img->filename, spec)) {
254                 delete in;
255                 return false;
256         }
257
258         /* we only handle certain number of components */
259         int width = spec.width;
260         int height = spec.height;
261         int components = spec.nchannels;
262
263         if(!(components == 1 || components == 3 || components == 4)) {
264                 in->close();
265                 delete in;
266                 return false;
267         }
268
269         /* read RGBA pixels */
270         uchar *pixels = (uchar*)tex_img.resize(width, height);
271         int scanlinesize = width*components*sizeof(uchar);
272
273         in->read_image(TypeDesc::UINT8,
274                 (uchar*)pixels + (height-1)*scanlinesize,
275                 AutoStride,
276                 -scanlinesize,
277                 AutoStride);
278
279         in->close();
280         delete in;
281
282         if(components == 3) {
283                 for(int i = width*height-1; i >= 0; i--) {
284                         pixels[i*4+3] = 255;
285                         pixels[i*4+2] = pixels[i*3+2];
286                         pixels[i*4+1] = pixels[i*3+1];
287                         pixels[i*4+0] = pixels[i*3+0];
288                 }
289         }
290         else if(components == 1) {
291                 for(int i = width*height-1; i >= 0; i--) {
292                         pixels[i*4+3] = 255;
293                         pixels[i*4+2] = pixels[i];
294                         pixels[i*4+1] = pixels[i];
295                         pixels[i*4+0] = pixels[i];
296                 }
297         }
298
299         return true;
300 }
301
302 bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_img)
303 {
304         if(img->filename == "")
305                 return false;
306
307         /* load image from file through OIIO */
308         ImageInput *in = ImageInput::create(img->filename);
309
310         if(!in)
311                 return false;
312
313         ImageSpec spec;
314
315         if(!in->open(img->filename, spec)) {
316                 delete in;
317                 return false;
318         }
319
320         /* we only handle certain number of components */
321         int width = spec.width;
322         int height = spec.height;
323         int components = spec.nchannels;
324
325         if(!(components == 1 || components == 3 || components == 4)) {
326                 in->close();
327                 delete in;
328                 return false;
329         }
330
331         /* read RGBA pixels */
332         float *pixels = (float*)tex_img.resize(width, height);
333         int scanlinesize = width*components*sizeof(float);
334
335         in->read_image(TypeDesc::FLOAT,
336                 (uchar*)pixels + (height-1)*scanlinesize,
337                 AutoStride,
338                 -scanlinesize,
339                 AutoStride);
340
341         in->close();
342         delete in;
343
344         if(components == 3) {
345                 for(int i = width*height-1; i >= 0; i--) {
346                         pixels[i*4+3] = 1.0f;
347                         pixels[i*4+2] = pixels[i*3+2];
348                         pixels[i*4+1] = pixels[i*3+1];
349                         pixels[i*4+0] = pixels[i*3+0];
350                 }
351         }
352         else if(components == 1) {
353                 for(int i = width*height-1; i >= 0; i--) {
354                         pixels[i*4+3] = 1.0f;
355                         pixels[i*4+2] = pixels[i];
356                         pixels[i*4+1] = pixels[i];
357                         pixels[i*4+0] = pixels[i];
358                 }
359         }
360
361         return true;
362 }
363
364 void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int slot, Progress *progress)
365 {
366         if(progress->get_cancel())
367                 return;
368         if(osl_texture_system)
369                 return;
370
371         Image *img;
372         bool is_float;
373
374         if(slot >= tex_image_byte_start) {
375                 img = images[slot - tex_image_byte_start];
376                 is_float = false;
377         }
378         else {
379                 img = float_images[slot];
380                 is_float = true;
381         }
382
383         if(is_float) {
384                 string filename = path_filename(float_images[slot]->filename);
385                 progress->set_status("Updating Images", "Loading " + filename);
386
387                 device_vector<float4>& tex_img = dscene->tex_float_image[slot];
388
389                 if(tex_img.device_pointer) {
390                         thread_scoped_lock device_lock(device_mutex);
391                         device->tex_free(tex_img);
392                 }
393
394                 if(!file_load_float_image(img, tex_img)) {
395                         /* on failure to load, we set a 1x1 pixels pink image */
396                         float *pixels = (float*)tex_img.resize(1, 1);
397
398                         pixels[0] = TEX_IMAGE_MISSING_R;
399                         pixels[1] = TEX_IMAGE_MISSING_G;
400                         pixels[2] = TEX_IMAGE_MISSING_B;
401                         pixels[3] = TEX_IMAGE_MISSING_A;
402                 }
403
404                 string name;
405
406                 if(slot >= 10) name = string_printf("__tex_image_float_0%d", slot);
407                 else name = string_printf("__tex_image_float_00%d", slot);
408
409                 if(!pack_images) {
410                         thread_scoped_lock device_lock(device_mutex);
411                         device->tex_alloc(name.c_str(), tex_img, true, true);
412                 }
413         }
414         else {
415                 string filename = path_filename(images[slot - tex_image_byte_start]->filename);
416                 progress->set_status("Updating Images", "Loading " + filename);
417
418                 device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
419
420                 if(tex_img.device_pointer) {
421                         thread_scoped_lock device_lock(device_mutex);
422                         device->tex_free(tex_img);
423                 }
424
425                 if(!file_load_image(img, tex_img)) {
426                         /* on failure to load, we set a 1x1 pixels pink image */
427                         uchar *pixels = (uchar*)tex_img.resize(1, 1);
428
429                         pixels[0] = (TEX_IMAGE_MISSING_R * 255);
430                         pixels[1] = (TEX_IMAGE_MISSING_G * 255);
431                         pixels[2] = (TEX_IMAGE_MISSING_B * 255);
432                         pixels[3] = (TEX_IMAGE_MISSING_A * 255);
433                 }
434
435                 string name;
436
437                 if(slot >= 10) name = string_printf("__tex_image_0%d", slot);
438                 else name = string_printf("__tex_image_00%d", slot);
439
440                 if(!pack_images) {
441                         thread_scoped_lock device_lock(device_mutex);
442                         device->tex_alloc(name.c_str(), tex_img, true, true);
443                 }
444         }
445
446         img->need_load = false;
447 }
448
449 void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int slot)
450 {
451         Image *img;
452         bool is_float;
453
454         if(slot >= tex_image_byte_start) {
455                 img = images[slot - tex_image_byte_start];
456                 is_float = false;
457         }
458         else {
459                 img = float_images[slot];
460                 is_float = true;
461         }
462
463         if(img) {
464                 if(osl_texture_system) {
465 #ifdef WITH_OSL
466                         ustring filename(images[slot]->filename);
467                         ((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
468 #endif
469                 }
470                 else if(is_float) {
471                         device_vector<float4>& tex_img = dscene->tex_float_image[slot];
472
473                         if(tex_img.device_pointer) {
474                                 thread_scoped_lock device_lock(device_mutex);
475                                 device->tex_free(tex_img);
476                         }
477
478                         tex_img.clear();
479
480                         delete float_images[slot];
481                         float_images[slot] = NULL;
482                 }
483                 else {
484                         device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
485
486                         if(tex_img.device_pointer) {
487                                 thread_scoped_lock device_lock(device_mutex);
488                                 device->tex_free(tex_img);
489                         }
490
491                         tex_img.clear();
492
493                         delete images[slot - tex_image_byte_start];
494                         images[slot - tex_image_byte_start] = NULL;
495                 }
496         }
497 }
498
499 void ImageManager::device_update(Device *device, DeviceScene *dscene, Progress& progress)
500 {
501         if(!need_update)
502                 return;
503
504         TaskPool pool;
505
506         for(size_t slot = 0; slot < images.size(); slot++) {
507                 if(!images[slot])
508                         continue;
509
510                 if(images[slot]->users == 0) {
511                         device_free_image(device, dscene, slot + tex_image_byte_start);
512                 }
513                 else if(images[slot]->need_load) {
514                         if(!osl_texture_system) 
515                                 pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot + tex_image_byte_start, &progress));
516                 }
517         }
518
519         for(size_t slot = 0; slot < float_images.size(); slot++) {
520                 if(!float_images[slot])
521                         continue;
522
523                 if(float_images[slot]->users == 0) {
524                         device_free_image(device, dscene, slot);
525                 }
526                 else if(float_images[slot]->need_load) {
527                         if(!osl_texture_system) 
528                                 pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot, &progress));
529                 }
530         }
531
532         pool.wait_work();
533
534         if(pack_images)
535                 device_pack_images(device, dscene, progress);
536
537         need_update = false;
538 }
539
540 void ImageManager::device_pack_images(Device *device, DeviceScene *dscene, Progress& progess)
541 {
542         /* for OpenCL, we pack all image textures inside a single big texture, and
543          * will do our own interpolation in the kernel */
544         size_t size = 0;
545
546         for(size_t slot = 0; slot < images.size(); slot++) {
547                 if(!images[slot])
548                         continue;
549
550                 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
551                 size += tex_img.size();
552         }
553
554         uint4 *info = dscene->tex_image_packed_info.resize(images.size());
555         uchar4 *pixels = dscene->tex_image_packed.resize(size);
556
557         size_t offset = 0;
558
559         for(size_t slot = 0; slot < images.size(); slot++) {
560                 if(!images[slot])
561                         continue;
562
563                 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
564
565                 info[slot] = make_uint4(tex_img.data_width, tex_img.data_height, offset, 1);
566
567                 memcpy(pixels+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
568                 offset += tex_img.size();
569         }
570
571         if(dscene->tex_image_packed.size())
572                 device->tex_alloc("__tex_image_packed", dscene->tex_image_packed);
573         if(dscene->tex_image_packed_info.size())
574                 device->tex_alloc("__tex_image_packed_info", dscene->tex_image_packed_info);
575 }
576
577 void ImageManager::device_free(Device *device, DeviceScene *dscene)
578 {
579         for(size_t slot = 0; slot < images.size(); slot++)
580                 device_free_image(device, dscene, slot + tex_image_byte_start);
581         for(size_t slot = 0; slot < float_images.size(); slot++)
582                 device_free_image(device, dscene, slot);
583
584         device->tex_free(dscene->tex_image_packed);
585         device->tex_free(dscene->tex_image_packed_info);
586
587         dscene->tex_image_packed.clear();
588         dscene->tex_image_packed_info.clear();
589
590         images.clear();
591         float_images.clear();
592 }
593
594 CCL_NAMESPACE_END
595