[Bf-blender-cvs] [72ab6faf5d8] master: Fix T97251: Store generated type information for each UDIM tile

Jesse Yurkovich noreply at git.blender.org
Thu Aug 4 07:31:03 CEST 2022


Commit: 72ab6faf5d80a3d26e5b6252e5b6f0041763209c
Author: Jesse Yurkovich
Date:   Wed Aug 3 22:00:52 2022 -0700
Branches: master
https://developer.blender.org/rB72ab6faf5d80a3d26e5b6252e5b6f0041763209c

Fix T97251: Store generated type information for each UDIM tile

Various situations can lead to un-saved UDIM tiles potentially losing
their contents. The most notable situation is a save and re-load of a
.blend file that has "generated" UDIM tiles that haven't been written to
disk yet. Normal "generated" images are reconstructed on demand in these
circumstances but UDIM tiles do not retain the information required for
reconstruction and empty tiles are presented to the user.

This patch stores the generated type information for each tile to solve
this particular issue. It also shifts the Image generation info into the
1st tile. The existing DNA fields are deprecated but RNA was modified as
to not break API compat.

There's two broad changes here that merit special callout:
- How to distinguish between a tile that should be reconstructed vs.
a tile that should remain empty because loading failed for the UDIMs
- How to better handle Image Source changes

The first issue is addressed as follows:
- Each time a tile is filled with generated content we set a new
IMA_GEN_TILE flag
- Each time a tile is saved to disk we remove the IMA_GEN_TILE flag
- When requesting an ibuf: If the ibuf is null, we check to see if
IMA_GEN_TILE is set. If it is set, go ahead and re-create the tile.
Otherwise, do nothing.

The second set of changes have to do with ensuring that information is
carried along as far as possible when the, sometimes destructive, act of
changing an Image Source is performed. Behavior should be a bit more
natural and expected now; though users will rarely, or should rarely, be
modifying this property. The full table describing the behavior is in
the differential.

Differential Revision: https://developer.blender.org/D14885

===================================================================

M	source/blender/blenkernel/BKE_image.h
M	source/blender/blenkernel/intern/image.cc
M	source/blender/blenkernel/intern/image_save.cc
M	source/blender/blenloader/intern/versioning_300.c
M	source/blender/editors/space_image/image_ops.c
M	source/blender/makesdna/DNA_image_types.h
M	source/blender/makesrna/intern/rna_image.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h
index 8512992282f..8c66e92f762 100644
--- a/source/blender/blenkernel/BKE_image.h
+++ b/source/blender/blenkernel/BKE_image.h
@@ -364,14 +364,7 @@ bool BKE_image_remove_tile(struct Image *ima, struct ImageTile *tile);
 void BKE_image_reassign_tile(struct Image *ima, struct ImageTile *tile, int new_tile_number);
 void BKE_image_sort_tiles(struct Image *ima);
 
-bool BKE_image_fill_tile(struct Image *ima,
-                         struct ImageTile *tile,
-                         int width,
-                         int height,
-                         const float color[4],
-                         int gen_type,
-                         int planes,
-                         bool is_float);
+bool BKE_image_fill_tile(struct Image *ima, struct ImageTile *tile);
 
 typedef enum {
   UDIM_TILE_FORMAT_NONE = 0,
diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index 4ce413d3705..f3e7ae449a1 100644
--- a/source/blender/blenkernel/intern/image.cc
+++ b/source/blender/blenkernel/intern/image.cc
@@ -627,6 +627,16 @@ void BKE_image_free_data(Image *ima)
   image_free_data(&ima->id);
 }
 
+static ImageTile *imagetile_alloc(int tile_number)
+{
+  ImageTile *tile = MEM_cnew<ImageTile>("Image Tile");
+  tile->tile_number = tile_number;
+  tile->gen_x = 1024;
+  tile->gen_y = 1024;
+  tile->gen_type = IMA_GENTYPE_GRID;
+  return tile;
+}
+
 /* only image block itself */
 static void image_init(Image *ima, short source, short type)
 {
@@ -641,8 +651,7 @@ static void image_init(Image *ima, short source, short type)
     ima->flag |= IMA_VIEW_AS_RENDER;
   }
 
-  ImageTile *tile = MEM_cnew<ImageTile>("Image Tiles");
-  tile->tile_number = 1001;
+  ImageTile *tile = imagetile_alloc(1001);
   BLI_addtail(&ima->tiles, tile);
 
   if (type == IMA_TYPE_R_RESULT) {
@@ -1100,73 +1109,70 @@ static void image_buf_fill_isolated(void *usersata_v)
   }
 }
 
-static ImBuf *add_ibuf_size(unsigned int width,
-                            unsigned int height,
-                            const char *name,
-                            int depth,
-                            int floatbuf,
-                            short gen_type,
-                            const float color[4],
-                            ColorManagedColorspaceSettings *colorspace_settings)
+static ImBuf *add_ibuf_for_tile(Image *ima, ImageTile *tile)
 {
   ImBuf *ibuf;
   unsigned char *rect = nullptr;
   float *rect_float = nullptr;
   float fill_color[4];
 
+  const bool floatbuf = (tile->gen_flag & IMA_GEN_FLOAT) != 0;
   if (floatbuf) {
-    ibuf = IMB_allocImBuf(width, height, depth, IB_rectfloat);
+    ibuf = IMB_allocImBuf(tile->gen_x, tile->gen_y, tile->gen_depth, IB_rectfloat);
 
-    if (colorspace_settings->name[0] == '\0') {
+    if (ima->colorspace_settings.name[0] == '\0') {
       const char *colorspace = IMB_colormanagement_role_colorspace_name_get(
           COLOR_ROLE_DEFAULT_FLOAT);
 
-      STRNCPY(colorspace_settings->name, colorspace);
+      STRNCPY(ima->colorspace_settings.name, colorspace);
     }
 
     if (ibuf != nullptr) {
       rect_float = ibuf->rect_float;
-      IMB_colormanagement_check_is_data(ibuf, colorspace_settings->name);
+      IMB_colormanagement_check_is_data(ibuf, ima->colorspace_settings.name);
     }
 
-    if (IMB_colormanagement_space_name_is_data(colorspace_settings->name)) {
-      copy_v4_v4(fill_color, color);
+    if (IMB_colormanagement_space_name_is_data(ima->colorspace_settings.name)) {
+      copy_v4_v4(fill_color, tile->gen_color);
     }
     else {
       /* The input color here should ideally be linear already, but for now
        * we just convert and postpone breaking the API for later. */
-      srgb_to_linearrgb_v4(fill_color, color);
+      srgb_to_linearrgb_v4(fill_color, tile->gen_color);
     }
   }
   else {
-    ibuf = IMB_allocImBuf(width, height, depth, IB_rect);
+    ibuf = IMB_allocImBuf(tile->gen_x, tile->gen_y, tile->gen_depth, IB_rect);
 
-    if (colorspace_settings->name[0] == '\0') {
+    if (ima->colorspace_settings.name[0] == '\0') {
       const char *colorspace = IMB_colormanagement_role_colorspace_name_get(
           COLOR_ROLE_DEFAULT_BYTE);
 
-      STRNCPY(colorspace_settings->name, colorspace);
+      STRNCPY(ima->colorspace_settings.name, colorspace);
     }
 
     if (ibuf != nullptr) {
       rect = (unsigned char *)ibuf->rect;
-      IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace_settings->name);
+      IMB_colormanagement_assign_rect_colorspace(ibuf, ima->colorspace_settings.name);
     }
 
-    copy_v4_v4(fill_color, color);
+    copy_v4_v4(fill_color, tile->gen_color);
   }
 
   if (!ibuf) {
     return nullptr;
   }
 
-  STRNCPY(ibuf->name, name);
+  STRNCPY(ibuf->name, ima->filepath);
+
+  /* Mark the tile itself as having been generated. */
+  tile->gen_flag |= IMA_GEN_TILE;
 
   ImageFillData data;
 
-  data.gen_type = gen_type;
-  data.width = width;
-  data.height = height;
+  data.gen_type = tile->gen_type;
+  data.width = tile->gen_x;
+  data.height = tile->gen_y;
   data.rect = rect;
   data.rect_float = rect_float;
   copy_v4_v4(data.fill_color, fill_color);
@@ -1205,12 +1211,16 @@ Image *BKE_image_add_generated(Main *bmain,
 
   /* NOTE: leave `ima->filepath` unset,
    * setting it to a dummy value may write to an invalid file-path. */
-  ima->gen_x = width;
-  ima->gen_y = height;
-  ima->gen_type = gen_type;
-  ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
-  ima->gen_depth = depth;
-  copy_v4_v4(ima->gen_color, color);
+
+  /* The generation info is always stored in the tiles. The first tile
+   * will be used for non-tiled images. */
+  ImageTile *tile = static_cast<ImageTile *>(ima->tiles.first);
+  tile->gen_x = width;
+  tile->gen_y = height;
+  tile->gen_type = gen_type;
+  tile->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
+  tile->gen_depth = depth;
+  copy_v4_v4(tile->gen_color, color);
 
   if (is_data) {
     STRNCPY(ima->colorspace_settings.name,
@@ -1219,8 +1229,7 @@ Image *BKE_image_add_generated(Main *bmain,
 
   for (view_id = 0; view_id < 2; view_id++) {
     ImBuf *ibuf;
-    ibuf = add_ibuf_size(
-        width, height, ima->filepath, depth, floatbuf, gen_type, color, &ima->colorspace_settings);
+    ibuf = add_ibuf_for_tile(ima, tile);
     int index = tiled ? 0 : IMA_NO_INDEX;
     int entry = tiled ? 1001 : 0;
     image_assign_ibuf(ima, ibuf, stereo3d ? view_id : index, entry);
@@ -3001,6 +3010,28 @@ static void image_free_tile(Image *ima, ImageTile *tile)
   }
 }
 
+static bool image_remove_tile(Image *ima, ImageTile *tile)
+{
+  if (BLI_listbase_is_single(&ima->tiles)) {
+    /* Can't remove the last remaining tile. */
+    return false;
+  }
+
+  image_free_tile(ima, tile);
+  BLI_remlink(&ima->tiles, tile);
+  MEM_freeN(tile);
+
+  return true;
+}
+
+static void image_remove_all_tiles(Image *ima)
+{
+  /* Remove all but the final tile. */
+  while (image_remove_tile(ima, static_cast<ImageTile *>(ima->tiles.last))) {
+    ;
+  }
+}
+
 void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
 {
   if (ima == nullptr) {
@@ -3027,11 +3058,12 @@ void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
       }
 
       if (ima->source == IMA_SRC_GENERATED) {
-        if (ima->gen_x == 0 || ima->gen_y == 0) {
+        ImageTile *base_tile = BKE_image_get_tile(ima, 0);
+        if (base_tile->gen_x == 0 || base_tile->gen_y == 0) {
           ImBuf *ibuf = image_get_cached_ibuf_for_index_entry(ima, IMA_NO_INDEX, 0, nullptr);
           if (ibuf) {
-            ima->gen_x = ibuf->x;
-            ima->gen_y = ibuf->y;
+            base_tile->gen_x = ibuf->x;
+            base_tile->gen_y = ibuf->y;
             IMB_freeImBuf(ibuf);
           }
         }
@@ -3048,16 +3080,40 @@ void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
 
       if (ima->source != IMA_SRC_TILED) {
         /* Free all but the first tile. */
+        image_remove_all_tiles(ima);
+
+        /* If the remaining tile is generated, we need to again ensure that we
+         * wouldn't continue to use the old filepath.
+         *
+         * Otherwise, if this used to be a UDIM image, get the concrete filepath associated
+         * with the remaining tile and use that as the new filepath. */
         ImageTile *base_tile = BKE_image_get_tile(ima, 0);
-        BLI_assert(base_tile == ima->tiles.first);
-        for (ImageTile *tile = base_tile->next, *tile_next; tile; tile = tile_next) {
-          tile_next = tile->next;
-          image_free_tile(ima, tile);
-          MEM_freeN(tile);
+        if ((base_tile->gen_flag & IMA_GEN_TILE) != 0) {
+          ima->filepath[0] = '\0';
         }
-        base_tile->next = nullptr;
+        else if (BKE_image_is_filename_tokenized(ima->filepath)) {
+          const bool was_relative = BLI_path_is_rel(ima->filepath);
+
+          eUDIM_TILE_FORMAT tile_format;
+          char *udim_pattern = BKE_image_get_tile_strformat(ima->filepath, &tile_format);
+          BKE_image_set_filepath_from_tile_number(
+              ima->filepath, udim_pattern, tile_format, base_tile->tile_number);
+          MEM_freeN(udim_pattern);
+
+          if (was_relative) {
+            const char *relbase = ID_BLEND_PATH(bmain, &ima->id);
+            BLI_path_rel(ima->filepath, relbase);
+          }
+        }
+
+        /* If the remaining tile was not number 1001, we need to reassign it so that
+         * ibuf lookups from the cache still succeed. */
         base_tile->tile_number = 1001;
-        ima->tiles.last = base_tile;
+      }
+      else {
+        /* When changing to UDIM, attempt to tokenize the filepath. */
+        char *filename = (char *)BLI_path_basename(ima->filepath);
+        BKE_image_ensure_tile_token(filename);
       }
 
       /* image buffers for non-sequence multilayer will share buffers with RenderResult,
@@ -3073,6 +3129,7 @@ void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
         image_tag_frame_recalc(ima, nullptr, iuser, ima);
       }
       BKE_image_walk_all_users(bmain, ima, image_tag_frame_recalc);
+   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list