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