Blender  V2.93
image.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "render/image.h"
18 #include "device/device.h"
19 #include "render/colorspace.h"
20 #include "render/image_oiio.h"
21 #include "render/image_vdb.h"
22 #include "render/scene.h"
23 #include "render/stats.h"
24 
25 #include "util/util_foreach.h"
26 #include "util/util_image.h"
27 #include "util/util_image_impl.h"
28 #include "util/util_logging.h"
29 #include "util/util_path.h"
30 #include "util/util_progress.h"
31 #include "util/util_task.h"
32 #include "util/util_texture.h"
33 #include "util/util_unique_ptr.h"
34 
35 #ifdef WITH_OSL
36 # include <OSL/oslexec.h>
37 #endif
38 
40 
41 namespace {
42 
43 /* Some helpers to silence warning in templated function. */
44 bool isfinite(uchar /*value*/)
45 {
46  return true;
47 }
48 bool isfinite(half /*value*/)
49 {
50  return true;
51 }
52 bool isfinite(uint16_t /*value*/)
53 {
54  return true;
55 }
56 
58 {
59  switch (type) {
61  return "float4";
63  return "byte4";
65  return "half4";
67  return "float";
69  return "byte";
71  return "half";
73  return "ushort4";
75  return "ushort";
77  return "nanovdb_float";
79  return "nanovdb_float3";
81  assert(!"System enumerator type, should never be used");
82  return "";
83  }
84  assert(!"Unhandled image data type");
85  return "";
86 }
87 
88 } // namespace
89 
90 /* Image Handle */
91 
93 {
94 }
95 
97  : tile_slots(other.tile_slots), manager(other.manager)
98 {
99  /* Increase image user count. */
100  foreach (const int slot, tile_slots) {
101  manager->add_image_user(slot);
102  }
103 }
104 
106 {
107  clear();
108  manager = other.manager;
109  tile_slots = other.tile_slots;
110 
111  foreach (const int slot, tile_slots) {
112  manager->add_image_user(slot);
113  }
114 
115  return *this;
116 }
117 
119 {
120  clear();
121 }
122 
124 {
125  foreach (const int slot, tile_slots) {
126  manager->remove_image_user(slot);
127  }
128 
129  tile_slots.clear();
130  manager = NULL;
131 }
132 
134 {
135  return tile_slots.empty();
136 }
137 
139 {
140  return tile_slots.size();
141 }
142 
144 {
145  if (tile_slots.empty()) {
146  return ImageMetaData();
147  }
148 
149  ImageManager::Image *img = manager->images[tile_slots.front()];
150  manager->load_image_metadata(img);
151  return img->metadata;
152 }
153 
154 int ImageHandle::svm_slot(const int tile_index) const
155 {
156  if (tile_index >= tile_slots.size()) {
157  return -1;
158  }
159 
160  if (manager->osl_texture_system) {
161  ImageManager::Image *img = manager->images[tile_slots[tile_index]];
162  if (!img->loader->osl_filepath().empty()) {
163  return -1;
164  }
165  }
166 
167  return tile_slots[tile_index];
168 }
169 
170 device_texture *ImageHandle::image_memory(const int tile_index) const
171 {
172  if (tile_index >= tile_slots.size()) {
173  return NULL;
174  }
175 
176  ImageManager::Image *img = manager->images[tile_slots[tile_index]];
177  return img ? img->mem : NULL;
178 }
179 
180 VDBImageLoader *ImageHandle::vdb_loader(const int tile_index) const
181 {
182  if (tile_index >= tile_slots.size()) {
183  return NULL;
184  }
185 
186  ImageManager::Image *img = manager->images[tile_slots[tile_index]];
187 
188  if (img == NULL) {
189  return NULL;
190  }
191 
192  ImageLoader *loader = img->loader;
193 
194  if (loader == NULL) {
195  return NULL;
196  }
197 
198  if (loader->is_vdb_loader()) {
199  return dynamic_cast<VDBImageLoader *>(loader);
200  }
201 
202  return NULL;
203 }
204 
205 bool ImageHandle::operator==(const ImageHandle &other) const
206 {
207  return manager == other.manager && tile_slots == other.tile_slots;
208 }
209 
210 /* Image MetaData */
211 
213  : channels(0),
214  width(0),
215  height(0),
216  depth(0),
217  byte_size(0),
219  colorspace(u_colorspace_raw),
220  colorspace_file_format(""),
221  use_transform_3d(false),
222  compress_as_srgb(false)
223 {
224 }
225 
226 bool ImageMetaData::operator==(const ImageMetaData &other) const
227 {
228  return channels == other.channels && width == other.width && height == other.height &&
229  depth == other.depth && use_transform_3d == other.use_transform_3d &&
230  (!use_transform_3d || transform_3d == other.transform_3d) && type == other.type &&
232 }
233 
235 {
238 }
239 
241 {
242  /* Convert used specified color spaces to one we know how to handle. */
245 
246  if (colorspace == u_colorspace_raw) {
247  /* Nothing to do. */
248  }
249  else if (colorspace == u_colorspace_srgb) {
250  /* Keep sRGB colorspace stored as sRGB, to save memory and/or loading time
251  * for the common case of 8bit sRGB images like PNG. */
252  compress_as_srgb = true;
253  }
254  else {
255  /* Always compress non-raw 8bit images as scene linear + sRGB, as a
256  * heuristic to keep memory usage the same without too much data loss
257  * due to quantization in common cases. */
259 
260  /* If colorspace conversion needed, use half instead of short so we can
261  * represent HDR values that might result from conversion. */
262  if (type == IMAGE_DATA_TYPE_USHORT) {
264  }
265  else if (type == IMAGE_DATA_TYPE_USHORT4) {
267  }
268  }
269 }
270 
271 /* Image Loader */
272 
274 {
275 }
276 
278 {
279  return ustring();
280 }
281 
283 {
284  if (a == NULL && b == NULL) {
285  return true;
286  }
287  else {
288  return (a && b && typeid(*a) == typeid(*b) && a->equals(*b));
289  }
290 }
291 
293 {
294  return false;
295 }
296 
297 /* Image Manager */
298 
300 {
301  need_update_ = true;
302  osl_texture_system = NULL;
303  animation_frame = 0;
304 
305  /* Set image limits */
306  features.has_half_float = info.has_half_images;
307  features.has_nanovdb = info.has_nanovdb;
308 }
309 
311 {
312  for (size_t slot = 0; slot < images.size(); slot++)
313  assert(!images[slot]);
314 }
315 
316 void ImageManager::set_osl_texture_system(void *texture_system)
317 {
318  osl_texture_system = texture_system;
319 }
320 
322 {
323  if (frame != animation_frame) {
324  thread_scoped_lock device_lock(images_mutex);
325  animation_frame = frame;
326 
327  for (size_t slot = 0; slot < images.size(); slot++) {
328  if (images[slot] && images[slot]->params.animated)
329  return true;
330  }
331  }
332 
333  return false;
334 }
335 
336 void ImageManager::load_image_metadata(Image *img)
337 {
338  if (!img->need_metadata) {
339  return;
340  }
341 
342  thread_scoped_lock image_lock(img->mutex);
343  if (!img->need_metadata) {
344  return;
345  }
346 
347  ImageMetaData &metadata = img->metadata;
348  metadata = ImageMetaData();
349  metadata.colorspace = img->params.colorspace;
350 
351  if (img->loader->load_metadata(features, metadata)) {
352  assert(metadata.type != IMAGE_DATA_NUM_TYPES);
353  }
354  else {
355  metadata.type = IMAGE_DATA_TYPE_BYTE4;
356  }
357 
358  metadata.detect_colorspace();
359 
360  assert(features.has_half_float ||
361  (metadata.type != IMAGE_DATA_TYPE_HALF4 && metadata.type != IMAGE_DATA_TYPE_HALF));
362  assert(features.has_nanovdb || (metadata.type != IMAGE_DATA_TYPE_NANOVDB_FLOAT ||
363  metadata.type != IMAGE_DATA_TYPE_NANOVDB_FLOAT3));
364 
365  img->need_metadata = false;
366 }
367 
368 ImageHandle ImageManager::add_image(const string &filename, const ImageParams &params)
369 {
370  const int slot = add_image_slot(new OIIOImageLoader(filename), params, false);
371 
372  ImageHandle handle;
373  handle.tile_slots.push_back(slot);
374  handle.manager = this;
375  return handle;
376 }
377 
378 ImageHandle ImageManager::add_image(const string &filename,
379  const ImageParams &params,
380  const array<int> &tiles)
381 {
382  ImageHandle handle;
383  handle.manager = this;
384 
385  foreach (int tile, tiles) {
386  string tile_filename = filename;
387  if (tile != 0) {
388  string_replace(tile_filename, "<UDIM>", string_printf("%04d", tile));
389  }
390  const int slot = add_image_slot(new OIIOImageLoader(tile_filename), params, false);
391  handle.tile_slots.push_back(slot);
392  }
393 
394  return handle;
395 }
396 
398  const ImageParams &params,
399  const bool builtin)
400 {
401  const int slot = add_image_slot(loader, params, builtin);
402 
403  ImageHandle handle;
404  handle.tile_slots.push_back(slot);
405  handle.manager = this;
406  return handle;
407 }
408 
409 int ImageManager::add_image_slot(ImageLoader *loader,
410  const ImageParams &params,
411  const bool builtin)
412 {
413  Image *img;
414  size_t slot;
415 
416  thread_scoped_lock device_lock(images_mutex);
417 
418  /* Find existing image. */
419  for (slot = 0; slot < images.size(); slot++) {
420  img = images[slot];
421  if (img && ImageLoader::equals(img->loader, loader) && img->params == params) {
422  img->users++;
423  delete loader;
424  return slot;
425  }
426  }
427 
428  /* Find free slot. */
429  for (slot = 0; slot < images.size(); slot++) {
430  if (!images[slot])
431  break;
432  }
433 
434  if (slot == images.size()) {
435  images.resize(images.size() + 1);
436  }
437 
438  /* Add new image. */
439  img = new Image();
440  img->params = params;
441  img->loader = loader;
442  img->need_metadata = true;
443  img->need_load = !(osl_texture_system && !img->loader->osl_filepath().empty());
444  img->builtin = builtin;
445  img->users = 1;
446  img->mem = NULL;
447 
448  images[slot] = img;
449 
450  need_update_ = true;
451 
452  return slot;
453 }
454 
455 void ImageManager::add_image_user(int slot)
456 {
457  thread_scoped_lock device_lock(images_mutex);
458  Image *image = images[slot];
459  assert(image && image->users >= 1);
460 
461  image->users++;
462 }
463 
464 void ImageManager::remove_image_user(int slot)
465 {
466  thread_scoped_lock device_lock(images_mutex);
467  Image *image = images[slot];
468  assert(image && image->users >= 1);
469 
470  /* decrement user count */
471  image->users--;
472 
473  /* don't remove immediately, rather do it all together later on. one of
474  * the reasons for this is that on shader changes we add and remove nodes
475  * that use them, but we do not want to reload the image all the time. */
476  if (image->users == 0)
477  need_update_ = true;
478 }
479 
481 {
482  /* For typical RGBA images we let OIIO convert to associated alpha,
483  * but some types we want to leave the RGB channels untouched. */
487 }
488 
489 template<TypeDesc::BASETYPE FileFormat, typename StorageType>
490 bool ImageManager::file_load_image(Image *img, int texture_limit)
491 {
492  /* Ignore empty images. */
493  if (!(img->metadata.channels > 0)) {
494  return false;
495  }
496 
497  /* Get metadata. */
498  int width = img->metadata.width;
499  int height = img->metadata.height;
500  int depth = img->metadata.depth;
501  int components = img->metadata.channels;
502 
503  /* Read pixels. */
504  vector<StorageType> pixels_storage;
505  StorageType *pixels;
506  const size_t max_size = max(max(width, height), depth);
507  if (max_size == 0) {
508  /* Don't bother with empty images. */
509  return false;
510  }
511 
512  /* Allocate memory as needed, may be smaller to resize down. */
513  if (texture_limit > 0 && max_size > texture_limit) {
514  pixels_storage.resize(((size_t)width) * height * depth * 4);
515  pixels = &pixels_storage[0];
516  }
517  else {
518  thread_scoped_lock device_lock(device_mutex);
519  pixels = (StorageType *)img->mem->alloc(width, height, depth);
520  }
521 
522  if (pixels == NULL) {
523  /* Could be that we've run out of memory. */
524  return false;
525  }
526 
527  const size_t num_pixels = ((size_t)width) * height * depth;
528  img->loader->load_pixels(
529  img->metadata, pixels, num_pixels * components, image_associate_alpha(img));
530 
531  /* The kernel can handle 1 and 4 channel images. Anything that is not a single
532  * channel image is converted to RGBA format. */
533  bool is_rgba = (img->metadata.type == IMAGE_DATA_TYPE_FLOAT4 ||
534  img->metadata.type == IMAGE_DATA_TYPE_HALF4 ||
535  img->metadata.type == IMAGE_DATA_TYPE_BYTE4 ||
536  img->metadata.type == IMAGE_DATA_TYPE_USHORT4);
537 
538  if (is_rgba) {
539  const StorageType one = util_image_cast_from_float<StorageType>(1.0f);
540 
541  if (components == 2) {
542  /* Grayscale + alpha to RGBA. */
543  for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
544  pixels[i * 4 + 3] = pixels[i * 2 + 1];
545  pixels[i * 4 + 2] = pixels[i * 2 + 0];
546  pixels[i * 4 + 1] = pixels[i * 2 + 0];
547  pixels[i * 4 + 0] = pixels[i * 2 + 0];
548  }
549  }
550  else if (components == 3) {
551  /* RGB to RGBA. */
552  for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
553  pixels[i * 4 + 3] = one;
554  pixels[i * 4 + 2] = pixels[i * 3 + 2];
555  pixels[i * 4 + 1] = pixels[i * 3 + 1];
556  pixels[i * 4 + 0] = pixels[i * 3 + 0];
557  }
558  }
559  else if (components == 1) {
560  /* Grayscale to RGBA. */
561  for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
562  pixels[i * 4 + 3] = one;
563  pixels[i * 4 + 2] = pixels[i];
564  pixels[i * 4 + 1] = pixels[i];
565  pixels[i * 4 + 0] = pixels[i];
566  }
567  }
568 
569  /* Disable alpha if requested by the user. */
570  if (img->params.alpha_type == IMAGE_ALPHA_IGNORE) {
571  for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
572  pixels[i * 4 + 3] = one;
573  }
574  }
575 
576  if (img->metadata.colorspace != u_colorspace_raw &&
577  img->metadata.colorspace != u_colorspace_srgb) {
578  /* Convert to scene linear. */
580  img->metadata.colorspace, pixels, num_pixels, img->metadata.compress_as_srgb);
581  }
582  }
583 
584  /* Make sure we don't have buggy values. */
585  if (FileFormat == TypeDesc::FLOAT) {
586  /* For RGBA buffers we put all channels to 0 if either of them is not
587  * finite. This way we avoid possible artifacts caused by fully changed
588  * hue. */
589  if (is_rgba) {
590  for (size_t i = 0; i < num_pixels; i += 4) {
591  StorageType *pixel = &pixels[i * 4];
592  if (!isfinite(pixel[0]) || !isfinite(pixel[1]) || !isfinite(pixel[2]) ||
593  !isfinite(pixel[3])) {
594  pixel[0] = 0;
595  pixel[1] = 0;
596  pixel[2] = 0;
597  pixel[3] = 0;
598  }
599  }
600  }
601  else {
602  for (size_t i = 0; i < num_pixels; ++i) {
603  StorageType *pixel = &pixels[i];
604  if (!isfinite(pixel[0])) {
605  pixel[0] = 0;
606  }
607  }
608  }
609  }
610 
611  /* Scale image down if needed. */
612  if (pixels_storage.size() > 0) {
613  float scale_factor = 1.0f;
614  while (max_size * scale_factor > texture_limit) {
615  scale_factor *= 0.5f;
616  }
617  VLOG(1) << "Scaling image " << img->loader->name() << " by a factor of " << scale_factor
618  << ".";
619  vector<StorageType> scaled_pixels;
620  size_t scaled_width, scaled_height, scaled_depth;
621  util_image_resize_pixels(pixels_storage,
622  width,
623  height,
624  depth,
625  is_rgba ? 4 : 1,
626  scale_factor,
627  &scaled_pixels,
628  &scaled_width,
629  &scaled_height,
630  &scaled_depth);
631 
632  StorageType *texture_pixels;
633 
634  {
635  thread_scoped_lock device_lock(device_mutex);
636  texture_pixels = (StorageType *)img->mem->alloc(scaled_width, scaled_height, scaled_depth);
637  }
638 
639  memcpy(texture_pixels, &scaled_pixels[0], scaled_pixels.size() * sizeof(StorageType));
640  }
641 
642  return true;
643 }
644 
645 void ImageManager::device_load_image(Device *device, Scene *scene, int slot, Progress *progress)
646 {
647  if (progress->get_cancel()) {
648  return;
649  }
650 
651  Image *img = images[slot];
652 
653  progress->set_status("Updating Images", "Loading " + img->loader->name());
654 
655  const int texture_limit = scene->params.texture_limit;
656 
657  load_image_metadata(img);
658  ImageDataType type = img->metadata.type;
659 
660  /* Name for debugging. */
661  img->mem_name = string_printf("__tex_image_%s_%03d", name_from_type(type), slot);
662 
663  /* Free previous texture in slot. */
664  if (img->mem) {
665  thread_scoped_lock device_lock(device_mutex);
666  delete img->mem;
667  img->mem = NULL;
668  }
669 
670  img->mem = new device_texture(
671  device, img->mem_name.c_str(), slot, type, img->params.interpolation, img->params.extension);
672  img->mem->info.use_transform_3d = img->metadata.use_transform_3d;
673  img->mem->info.transform_3d = img->metadata.transform_3d;
674 
675  /* Create new texture. */
676  if (type == IMAGE_DATA_TYPE_FLOAT4) {
677  if (!file_load_image<TypeDesc::FLOAT, float>(img, texture_limit)) {
678  /* on failure to load, we set a 1x1 pixels pink image */
679  thread_scoped_lock device_lock(device_mutex);
680  float *pixels = (float *)img->mem->alloc(1, 1);
681 
682  pixels[0] = TEX_IMAGE_MISSING_R;
683  pixels[1] = TEX_IMAGE_MISSING_G;
684  pixels[2] = TEX_IMAGE_MISSING_B;
685  pixels[3] = TEX_IMAGE_MISSING_A;
686  }
687  }
688  else if (type == IMAGE_DATA_TYPE_FLOAT) {
689  if (!file_load_image<TypeDesc::FLOAT, float>(img, texture_limit)) {
690  /* on failure to load, we set a 1x1 pixels pink image */
691  thread_scoped_lock device_lock(device_mutex);
692  float *pixels = (float *)img->mem->alloc(1, 1);
693 
694  pixels[0] = TEX_IMAGE_MISSING_R;
695  }
696  }
697  else if (type == IMAGE_DATA_TYPE_BYTE4) {
698  if (!file_load_image<TypeDesc::UINT8, uchar>(img, texture_limit)) {
699  /* on failure to load, we set a 1x1 pixels pink image */
700  thread_scoped_lock device_lock(device_mutex);
701  uchar *pixels = (uchar *)img->mem->alloc(1, 1);
702 
703  pixels[0] = (TEX_IMAGE_MISSING_R * 255);
704  pixels[1] = (TEX_IMAGE_MISSING_G * 255);
705  pixels[2] = (TEX_IMAGE_MISSING_B * 255);
706  pixels[3] = (TEX_IMAGE_MISSING_A * 255);
707  }
708  }
709  else if (type == IMAGE_DATA_TYPE_BYTE) {
710  if (!file_load_image<TypeDesc::UINT8, uchar>(img, texture_limit)) {
711  /* on failure to load, we set a 1x1 pixels pink image */
712  thread_scoped_lock device_lock(device_mutex);
713  uchar *pixels = (uchar *)img->mem->alloc(1, 1);
714 
715  pixels[0] = (TEX_IMAGE_MISSING_R * 255);
716  }
717  }
718  else if (type == IMAGE_DATA_TYPE_HALF4) {
719  if (!file_load_image<TypeDesc::HALF, half>(img, texture_limit)) {
720  /* on failure to load, we set a 1x1 pixels pink image */
721  thread_scoped_lock device_lock(device_mutex);
722  half *pixels = (half *)img->mem->alloc(1, 1);
723 
724  pixels[0] = TEX_IMAGE_MISSING_R;
725  pixels[1] = TEX_IMAGE_MISSING_G;
726  pixels[2] = TEX_IMAGE_MISSING_B;
727  pixels[3] = TEX_IMAGE_MISSING_A;
728  }
729  }
730  else if (type == IMAGE_DATA_TYPE_USHORT) {
731  if (!file_load_image<TypeDesc::USHORT, uint16_t>(img, texture_limit)) {
732  /* on failure to load, we set a 1x1 pixels pink image */
733  thread_scoped_lock device_lock(device_mutex);
734  uint16_t *pixels = (uint16_t *)img->mem->alloc(1, 1);
735 
736  pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
737  }
738  }
739  else if (type == IMAGE_DATA_TYPE_USHORT4) {
740  if (!file_load_image<TypeDesc::USHORT, uint16_t>(img, texture_limit)) {
741  /* on failure to load, we set a 1x1 pixels pink image */
742  thread_scoped_lock device_lock(device_mutex);
743  uint16_t *pixels = (uint16_t *)img->mem->alloc(1, 1);
744 
745  pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
746  pixels[1] = (TEX_IMAGE_MISSING_G * 65535);
747  pixels[2] = (TEX_IMAGE_MISSING_B * 65535);
748  pixels[3] = (TEX_IMAGE_MISSING_A * 65535);
749  }
750  }
751  else if (type == IMAGE_DATA_TYPE_HALF) {
752  if (!file_load_image<TypeDesc::HALF, half>(img, texture_limit)) {
753  /* on failure to load, we set a 1x1 pixels pink image */
754  thread_scoped_lock device_lock(device_mutex);
755  half *pixels = (half *)img->mem->alloc(1, 1);
756 
757  pixels[0] = TEX_IMAGE_MISSING_R;
758  }
759  }
760 #ifdef WITH_NANOVDB
762  thread_scoped_lock device_lock(device_mutex);
763  void *pixels = img->mem->alloc(img->metadata.byte_size, 0);
764 
765  if (pixels != NULL) {
766  img->loader->load_pixels(img->metadata, pixels, img->metadata.byte_size, false);
767  }
768  }
769 #endif
770 
771  {
772  thread_scoped_lock device_lock(device_mutex);
773  img->mem->copy_to_device();
774  }
775 
776  /* Cleanup memory in image loader. */
777  img->loader->cleanup();
778  img->need_load = false;
779 }
780 
781 void ImageManager::device_free_image(Device *, int slot)
782 {
783  Image *img = images[slot];
784  if (img == NULL) {
785  return;
786  }
787 
788  if (osl_texture_system) {
789 #ifdef WITH_OSL
790  ustring filepath = img->loader->osl_filepath();
791  if (!filepath.empty()) {
792  ((OSL::TextureSystem *)osl_texture_system)->invalidate(filepath);
793  }
794 #endif
795  }
796 
797  if (img->mem) {
798  thread_scoped_lock device_lock(device_mutex);
799  delete img->mem;
800  }
801 
802  delete img->loader;
803  delete img;
804  images[slot] = NULL;
805 }
806 
808 {
809  if (!need_update()) {
810  return;
811  }
812 
813  scoped_callback_timer timer([scene](double time) {
814  if (scene->update_stats) {
815  scene->update_stats->image.times.add_entry({"device_update", time});
816  }
817  });
818 
819  TaskPool pool;
820  for (size_t slot = 0; slot < images.size(); slot++) {
821  Image *img = images[slot];
822  if (img && img->users == 0) {
823  device_free_image(device, slot);
824  }
825  else if (img && img->need_load) {
826  pool.push(
827  function_bind(&ImageManager::device_load_image, this, device, scene, slot, &progress));
828  }
829  }
830 
831  pool.wait_work();
832 
833  need_update_ = false;
834 }
835 
836 void ImageManager::device_update_slot(Device *device, Scene *scene, int slot, Progress *progress)
837 {
838  Image *img = images[slot];
839  assert(img != NULL);
840 
841  if (img->users == 0) {
842  device_free_image(device, slot);
843  }
844  else if (img->need_load) {
845  device_load_image(device, scene, slot, progress);
846  }
847 }
848 
850 {
851  /* Load only builtin images, Blender needs this to load evaluated
852  * scene data from depsgraph before it is freed. */
853  if (!need_update()) {
854  return;
855  }
856 
857  TaskPool pool;
858  for (size_t slot = 0; slot < images.size(); slot++) {
859  Image *img = images[slot];
860  if (img && img->need_load && img->builtin) {
861  pool.push(
862  function_bind(&ImageManager::device_load_image, this, device, scene, slot, &progress));
863  }
864  }
865 
866  pool.wait_work();
867 }
868 
870 {
871  for (size_t slot = 0; slot < images.size(); slot++) {
872  Image *img = images[slot];
873  if (img && img->builtin) {
874  device_free_image(device, slot);
875  }
876  }
877 }
878 
880 {
881  for (size_t slot = 0; slot < images.size(); slot++) {
882  device_free_image(device, slot);
883  }
884  images.clear();
885 }
886 
888 {
889  foreach (const Image *image, images) {
890  stats->image.textures.add_entry(
891  NamedSizeEntry(image->loader->name(), image->mem->memory_size()));
892  }
893 }
894 
896 {
897  need_update_ = true;
898 }
899 
901 {
902  return need_update_;
903 }
904 
unsigned char uchar
Definition: BLI_sys_types.h:86
struct Image Image
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
static bool colorspace_is_data(ustring colorspace)
Definition: colorspace.cpp:85
static void to_scene_linear(ustring colorspace, T *pixels, size_t num_pixels, bool compress_as_srgb)
Definition: colorspace.cpp:316
static ustring detect_known_colorspace(ustring colorspace, const char *file_format, bool is_float)
Definition: colorspace.cpp:110
bool has_half_images
Definition: device.h:79
bool has_nanovdb
Definition: device.h:80
Definition: device.h:293
~ImageHandle()
Definition: image.cpp:118
bool operator==(const ImageHandle &other) const
Definition: image.cpp:205
vector< int > tile_slots
VDBImageLoader * vdb_loader(const int tile_index=0) const
Definition: image.cpp:180
ImageHandle()
Definition: image.cpp:92
void clear()
Definition: image.cpp:123
device_texture * image_memory(const int tile_index=0) const
Definition: image.cpp:170
bool empty()
Definition: image.cpp:133
int num_tiles()
Definition: image.cpp:138
ImageManager * manager
ImageHandle & operator=(const ImageHandle &other)
Definition: image.cpp:105
ImageMetaData metadata()
Definition: image.cpp:143
int svm_slot(const int tile_index=0) const
Definition: image.cpp:154
virtual bool equals(const ImageLoader &other) const =0
virtual ustring osl_filepath() const
Definition: image.cpp:277
virtual string name() const =0
virtual bool is_vdb_loader() const
Definition: image.cpp:292
ImageLoader()
Definition: image.cpp:273
bool set_animation_frame_update(int frame)
Definition: image.cpp:321
void tag_update()
Definition: image.cpp:895
~ImageManager()
Definition: image.cpp:310
ImageManager(const DeviceInfo &info)
Definition: image.cpp:299
void device_update(Device *device, Scene *scene, Progress &progress)
Definition: image.cpp:807
bool need_update() const
Definition: image.cpp:900
void device_free(Device *device)
Definition: image.cpp:879
void device_load_builtin(Device *device, Scene *scene, Progress &progress)
Definition: image.cpp:849
void device_free_builtin(Device *device)
Definition: image.cpp:869
ImageHandle add_image(const string &filename, const ImageParams &params)
Definition: image.cpp:368
void device_update_slot(Device *device, Scene *scene, int slot, Progress *progress)
Definition: image.cpp:836
void collect_statistics(RenderStats *stats)
Definition: image.cpp:887
void set_osl_texture_system(void *texture_system)
Definition: image.cpp:316
ImageDataType type
bool is_float() const
Definition: image.cpp:234
Transform transform_3d
void detect_colorspace()
Definition: image.cpp:240
bool operator==(const ImageMetaData &other) const
Definition: image.cpp:226
const char * colorspace_file_format
ImageAlphaType alpha_type
NamedSizeStats textures
Definition: stats.h:173
short type
uint height() const
Definition: Image.cpp:63
uint width() const
Definition: Image.cpp:58
void add_entry(const NamedSizeEntry &entry)
Definition: stats.cpp:78
bool get_cancel()
void set_status(const string &status_, const string &substatus_="")
int texture_limit
Definition: scene.h:181
size_t memory_size()
Definition: util_half.h:41
ustring u_colorspace_raw
ustring u_colorspace_srgb
double time
Scene scene
#define function_bind
static bool image_associate_alpha(ImageManager::Image *img)
Definition: image.cpp:480
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define CCL_NAMESPACE_END
bool isfinite(uint16_t)
Definition: image.cpp:52
const char * name_from_type(ImageDataType type)
Definition: image.cpp:57
static unsigned a[3]
Definition: RandGen.cpp:92
unsigned short uint16_t
Definition: stdint.h:82
ImageStats image
Definition: stats.h:190
SceneParams params
Definition: scene.h:264
SceneUpdateStats * update_stats
Definition: scene.h:270
void push(TaskRunFunction &&task)
Definition: util_task.cpp:36
void wait_work(Summary *stats=NULL)
Definition: util_task.cpp:42
float max
CCL_NAMESPACE_BEGIN OIIO_NAMESPACE_USING void util_image_resize_pixels(const vector< T > &input_pixels, const size_t input_width, const size_t input_height, const size_t input_depth, const size_t components, vector< T > *output_pixels, size_t *output_width, size_t *output_height, size_t *output_depth)
#define VLOG(severity)
Definition: util_logging.h:50
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition: util_string.cpp:32
void string_replace(string &haystack, const string &needle, const string &other)
ImageDataType
Definition: util_texture.h:51
@ IMAGE_DATA_NUM_TYPES
Definition: util_texture.h:63
@ IMAGE_DATA_TYPE_BYTE
Definition: util_texture.h:56
@ IMAGE_DATA_TYPE_FLOAT
Definition: util_texture.h:55
@ IMAGE_DATA_TYPE_FLOAT4
Definition: util_texture.h:52
@ IMAGE_DATA_TYPE_USHORT4
Definition: util_texture.h:58
@ IMAGE_DATA_TYPE_USHORT
Definition: util_texture.h:59
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition: util_texture.h:60
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition: util_texture.h:61
@ IMAGE_DATA_TYPE_HALF
Definition: util_texture.h:57
@ IMAGE_DATA_TYPE_BYTE4
Definition: util_texture.h:53
@ IMAGE_DATA_TYPE_HALF4
Definition: util_texture.h:54
@ IMAGE_ALPHA_CHANNEL_PACKED
Definition: util_texture.h:71
@ IMAGE_ALPHA_IGNORE
Definition: util_texture.h:72
#define TEX_IMAGE_MISSING_R
Definition: util_texture.h:28
#define TEX_IMAGE_MISSING_B
Definition: util_texture.h:30
#define TEX_IMAGE_MISSING_A
Definition: util_texture.h:31
#define TEX_IMAGE_MISSING_G
Definition: util_texture.h:29
std::unique_lock< std::mutex > thread_scoped_lock
Definition: util_thread.h:41