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