2 * Copyright 2011, Blender Foundation.
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.
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.
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.
23 #include "util_foreach.h"
24 #include "util_image.h"
25 #include "util_path.h"
26 #include "util_progress.h"
29 #include <OSL/oslexec.h>
34 ImageManager::ImageManager()
38 osl_texture_system = NULL;
41 ImageManager::~ImageManager()
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]);
49 void ImageManager::set_pack_images(bool pack_images_)
51 pack_images = pack_images_;
54 void ImageManager::set_osl_texture_system(void *texture_system)
56 osl_texture_system = texture_system;
59 static bool is_float_image(const string& filename)
61 ImageInput *in = ImageInput::create(filename);
62 bool is_float = false;
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)
73 for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
74 if(spec.channelformats[channel].basesize() > 1)
87 int ImageManager::add_image(const string& filename, bool& is_float)
92 /* load image info and find out if we need a float texture */
93 is_float = (pack_images)? false: is_float_image(filename);
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;
105 for(slot = 0; slot < float_images.size(); slot++) {
106 if(!float_images[slot])
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());
118 float_images.resize(float_images.size() + 1);
123 img->filename = filename;
124 img->need_load = true;
127 float_images[slot] = img;
128 /* report slot out of total set of textures */
129 slot += TEX_IMAGE_FLOAT_START;
132 for(slot = 0; slot < images.size(); slot++) {
133 if(images[slot] && images[slot]->filename == filename) {
134 images[slot]->users++;
140 for(slot = 0; slot < images.size(); slot++) {
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());
153 images.resize(images.size() + 1);
158 img->filename = filename;
159 img->need_load = true;
169 void ImageManager::remove_image(const string& filename)
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);
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)
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);
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)
209 bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
211 if(img->filename == "")
214 /* load image from file through OIIO */
215 ImageInput *in = ImageInput::create(img->filename);
222 if(!in->open(img->filename, spec)) {
227 /* we only handle certain number of components */
228 int width = spec.width;
229 int height = spec.height;
230 int components = spec.nchannels;
232 if(!(components == 1 || components == 3 || components == 4)) {
238 /* read RGBA pixels */
239 uchar *pixels = (uchar*)tex_img.resize(width, height);
240 int scanlinesize = width*components*sizeof(uchar);
242 in->read_image(TypeDesc::UINT8,
243 (uchar*)pixels + (height-1)*scanlinesize,
251 if(components == 3) {
252 for(int i = width*height-1; i >= 0; i--) {
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];
259 else if(components == 1) {
260 for(int i = width*height-1; i >= 0; i--) {
262 pixels[i*4+2] = pixels[i];
263 pixels[i*4+1] = pixels[i];
264 pixels[i*4+0] = pixels[i];
271 bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_img)
273 if(img->filename == "")
276 /* load image from file through OIIO */
277 ImageInput *in = ImageInput::create(img->filename);
284 if(!in->open(img->filename, spec)) {
289 /* we only handle certain number of components */
290 int width = spec.width;
291 int height = spec.height;
292 int components = spec.nchannels;
294 if(!(components == 1 || components == 3 || components == 4)) {
300 /* read RGBA pixels */
301 float *pixels = (float*)tex_img.resize(width, height);
302 int scanlinesize = width*components*sizeof(float);
304 in->read_image(TypeDesc::FLOAT,
305 (uchar*)pixels + (height-1)*scanlinesize,
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];
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];
333 void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int slot, Progress *progress)
335 if(progress->get_cancel())
337 if(osl_texture_system)
343 if(slot < TEX_IMAGE_FLOAT_START) {
348 img = float_images[slot - TEX_IMAGE_FLOAT_START];
353 string filename = path_filename(float_images[slot - TEX_IMAGE_FLOAT_START]->filename);
354 progress->set_status("Updating Images", "Loading " + filename);
356 device_vector<float4>& tex_img = dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START];
358 if(tex_img.device_pointer)
359 device->tex_free(tex_img);
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);
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;
373 if(slot >= 10) name = string_printf("__tex_image_float_0%d", slot);
374 else name = string_printf("__tex_image_float_00%d", slot);
377 device->tex_alloc(name.c_str(), tex_img, true, true);
380 string filename = path_filename(images[slot]->filename);
381 progress->set_status("Updating Images", "Loading " + filename);
383 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
385 if(tex_img.device_pointer)
386 device->tex_free(tex_img);
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);
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);
400 if(slot >= 10) name = string_printf("__tex_image_0%d", slot);
401 else name = string_printf("__tex_image_00%d", slot);
404 device->tex_alloc(name.c_str(), tex_img, true, true);
407 img->need_load = false;
410 void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int slot)
415 if(slot < TEX_IMAGE_FLOAT_START) {
420 img = float_images[slot - TEX_IMAGE_FLOAT_START];
425 if(osl_texture_system) {
427 ustring filename(images[slot]->filename);
428 ((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
432 device->tex_free(dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START]);
433 dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START].clear();
435 delete float_images[slot - TEX_IMAGE_FLOAT_START];
436 float_images[slot - TEX_IMAGE_FLOAT_START] = NULL;
439 device->tex_free(dscene->tex_image[slot]);
440 dscene->tex_image[slot].clear();
448 void ImageManager::device_update(Device *device, DeviceScene *dscene, Progress& progress)
455 for(size_t slot = 0; slot < images.size(); slot++) {
459 if(images[slot]->users == 0) {
460 device_free_image(device, dscene, slot);
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));
468 for(size_t slot = 0; slot < float_images.size(); slot++) {
469 if(!float_images[slot])
472 if(float_images[slot]->users == 0) {
473 device_free_image(device, dscene, slot + TEX_IMAGE_FLOAT_START);
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));
484 device_pack_images(device, dscene, progress);
489 void ImageManager::device_pack_images(Device *device, DeviceScene *dscene, Progress& progess)
491 /* for OpenCL, we pack all image textures inside a single big texture, and
492 * will do our own interpolation in the kernel */
495 for(size_t slot = 0; slot < images.size(); slot++) {
499 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
500 size += tex_img.size();
503 uint4 *info = dscene->tex_image_packed_info.resize(images.size());
504 uchar4 *pixels = dscene->tex_image_packed.resize(size);
508 for(size_t slot = 0; slot < images.size(); slot++) {
512 device_vector<uchar4>& tex_img = dscene->tex_image[slot];
514 info[slot] = make_uint4(tex_img.data_width, tex_img.data_height, offset, 1);
516 memcpy(pixels+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
517 offset += tex_img.size();
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);
526 void ImageManager::device_free(Device *device, DeviceScene *dscene)
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);
533 device->tex_free(dscene->tex_image_packed);
534 dscene->tex_image_packed.clear();
536 device->tex_free(dscene->tex_image_packed_info);
537 dscene->tex_image_packed_info.clear();
540 float_images.clear();