[Bf-blender-cvs] [0a32ac02e97] master: Image: Partial Update Redesign.

Jeroen Bakker noreply at git.blender.org
Fri Jan 28 08:06:45 CET 2022


Commit: 0a32ac02e976a4723802ed09bed64c0625e889a2
Author: Jeroen Bakker
Date:   Fri Jan 28 08:05:31 2022 +0100
Branches: master
https://developer.blender.org/rB0a32ac02e976a4723802ed09bed64c0625e889a2

Image: Partial Update Redesign.

This patch reimplements the image partial updates. Biggest design motivation for the redesign
is that currently GPUTextures must be owned by the image. This reduces flexibility and adds
complexity to a single component especially when we want to have different structures.

The new design is not limited to GPUTextures and can also be used by reducing overhead in image
operations like scaling. Or partial image updating in Cycles.

The usecase in hand is that we want to support virtual images in the image editor so we can
work with images that don't fit in a single GPUTexture.

Using `BKE_image_partial_update_mark_region` or `BKE_image_partial_update_mark_full_update`
a part of an image can be marked as dirty. These regions are stored per ImageTile (UDIM).

When a part of the code wants to receive partial changes it needs to construct a `PartialUpdateUser`
by calling `BKE_image_partial_update_create`. As long as this instance is kept alive the changes can
be received.

When a user wants to update its own data it will call `BKE_image_partial_update_collect_changes`
This will collect the changes since the last time the user called this function. When the partial changes
are available the partial change can be read by calling `BKE_image_partial_update_get_next_change`

It can happen that the introduced mechanism doesn't have the data anymore to construct the
changes since the last time a PartialUpdateUser requested it. In this case it will get a request
to perform a full update.

Maniphest Tasks: T92613

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

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

M	source/blender/blenkernel/BKE_image.h
A	source/blender/blenkernel/BKE_image_partial_update.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/image.c
M	source/blender/blenkernel/intern/image_gpu.cc
A	source/blender/blenkernel/intern/image_partial_update.cc
A	source/blender/blenkernel/intern/image_partial_update_test.cc
M	source/blender/editors/render/render_internal.cc
M	source/blender/makesdna/DNA_image_types.h

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

diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h
index 80c6b155be0..598818ba3c0 100644
--- a/source/blender/blenkernel/BKE_image.h
+++ b/source/blender/blenkernel/BKE_image.h
@@ -24,6 +24,8 @@
 
 #include "BLI_utildefines.h"
 
+#include "BLI_rect.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -561,19 +563,27 @@ struct GPUTexture *BKE_image_get_gpu_tilemap(struct Image *image,
  * Is the alpha of the `GPUTexture` for a given image/ibuf premultiplied.
  */
 bool BKE_image_has_gpu_texture_premultiplied_alpha(struct Image *image, struct ImBuf *ibuf);
+
 /**
  * Partial update of texture for texture painting.
  * This is often much quicker than fully updating the texture for high resolution images.
  */
 void BKE_image_update_gputexture(
     struct Image *ima, struct ImageUser *iuser, int x, int y, int w, int h);
+
 /**
  * Mark areas on the #GPUTexture that needs to be updated. The areas are marked in chunks.
  * The next time the #GPUTexture is used these tiles will be refreshes. This saves time
  * when writing to the same place multiple times This happens for during foreground rendering.
  */
-void BKE_image_update_gputexture_delayed(
-    struct Image *ima, struct ImBuf *ibuf, int x, int y, int w, int h);
+void BKE_image_update_gputexture_delayed(struct Image *ima,
+                                         struct ImageTile *image_tile,
+                                         struct ImBuf *ibuf,
+                                         int x,
+                                         int y,
+                                         int w,
+                                         int h);
+
 /**
  * Called on entering and exiting texture paint mode,
  * temporary disabling/enabling mipmapping on all images for quick texture
@@ -591,6 +601,32 @@ bool BKE_image_remove_renderslot(struct Image *ima, struct ImageUser *iuser, int
 struct RenderSlot *BKE_image_get_renderslot(struct Image *ima, int index);
 bool BKE_image_clear_renderslot(struct Image *ima, struct ImageUser *iuser, int slot);
 
+/* --- image_partial_update.cc --- */
+/** Image partial updates. */
+struct PartialUpdateUser;
+
+/**
+ * \brief Create a new PartialUpdateUser. An Object that contains data to use partial updates.
+ */
+struct PartialUpdateUser *BKE_image_partial_update_create(const struct Image *image);
+
+/**
+ * \brief free a partial update user.
+ */
+void BKE_image_partial_update_free(struct PartialUpdateUser *user);
+
+/* --- partial updater (image side) --- */
+struct PartialUpdateRegister;
+
+void BKE_image_partial_update_register_free(struct Image *image);
+/** \brief Mark a region of the image to update. */
+void BKE_image_partial_update_mark_region(struct Image *image,
+                                          const struct ImageTile *image_tile,
+                                          const struct ImBuf *image_buffer,
+                                          const rcti *updated_region);
+/** \brief Mark the whole image to be updated. */
+void BKE_image_partial_update_mark_full_update(struct Image *image);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/BKE_image_partial_update.hh b/source/blender/blenkernel/BKE_image_partial_update.hh
new file mode 100644
index 00000000000..6af44b2c3c9
--- /dev/null
+++ b/source/blender/blenkernel/BKE_image_partial_update.hh
@@ -0,0 +1,298 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Copyright 2021, Blender Foundation.
+ */
+
+/** \file
+ * \ingroup bke
+ *
+ * To reduce the overhead of image processing this file contains a mechanism to detect areas of the
+ * image that are changed. These areas are organized in chunks. Changes that happen over time are
+ * organized in changesets.
+ *
+ * A common usecase is to update GPUTexture for drawing where only that part is uploaded that only
+ * changed.
+ */
+
+#pragma once
+
+#include "BLI_utildefines.h"
+
+#include "BLI_rect.h"
+
+#include "DNA_image_types.h"
+
+extern "C" {
+struct PartialUpdateUser;
+struct PartialUpdateRegister;
+}
+
+namespace blender::bke::image {
+
+using TileNumber = int;
+
+namespace partial_update {
+
+/* --- image_partial_update.cc --- */
+/** Image partial updates. */
+
+/**
+ * \brief Result codes of #BKE_image_partial_update_collect_changes.
+ */
+enum class ePartialUpdateCollectResult {
+  /** \brief Unable to construct partial updates. Caller should perform a full update. */
+  FullUpdateNeeded,
+
+  /** \brief No changes detected since the last time requested. */
+  NoChangesDetected,
+
+  /** \brief Changes detected since the last time requested. */
+  PartialChangesDetected,
+};
+
+/**
+ * \brief A region to update.
+ *
+ * Data is organized in tiles. These tiles are in texel space (1 unit is a single texel). When
+ * tiles are requested they are merged with neighboring tiles.
+ */
+struct PartialUpdateRegion {
+  /** \brief region of the image that has been updated. Region can be bigger than actual changes.
+   */
+  struct rcti region;
+
+  /**
+   * \brief Tile number (UDIM) that this region belongs to.
+   */
+  TileNumber tile_number;
+};
+
+/**
+ * \brief Return codes of #BKE_image_partial_update_get_next_change.
+ */
+enum class ePartialUpdateIterResult {
+  /** \brief no tiles left when iterating over tiles. */
+  Finished = 0,
+
+  /** \brief a chunk was available and has been loaded. */
+  ChangeAvailable = 1,
+};
+
+/**
+ * \brief collect the partial update since the last request.
+ *
+ * Invoke #BKE_image_partial_update_get_next_change to iterate over the collected tiles.
+ *
+ * \returns ePartialUpdateCollectResult::FullUpdateNeeded: called should not use partial updates
+ * but recalculate the full image. This result can be expected when called for the first time for a
+ * user and when it isn't possible to reconstruct the changes as the internal state doesn't have
+ * enough data stored. ePartialUpdateCollectResult::NoChangesDetected: The have been no changes
+ * detected since last invoke for the same user.
+ * ePartialUpdateCollectResult::PartialChangesDetected: Parts of the image has been updated since
+ * last invoke for the same user. The changes can be read by using
+ * #BKE_image_partial_update_get_next_change.
+ */
+ePartialUpdateCollectResult BKE_image_partial_update_collect_changes(
+    struct Image *image, struct PartialUpdateUser *user);
+
+ePartialUpdateIterResult BKE_image_partial_update_get_next_change(
+    struct PartialUpdateUser *user, struct PartialUpdateRegion *r_region);
+
+/** \brief Abstract class to load tile data when using the PartialUpdateChecker. */
+class AbstractTileData {
+ protected:
+  virtual ~AbstractTileData() = default;
+
+ public:
+  /**
+   * \brief Load the data for the given tile_number.
+   *
+   * Invoked when changes are on a different tile compared to the previous tile..
+   */
+  virtual void init_data(TileNumber tile_number) = 0;
+  /**
+   * \brief Unload the data that has been loaded.
+   *
+   * Invoked when changes are on a different tile compared to the previous tile or when finished
+   * iterating over the changes.
+   */
+  virtual void free_data() = 0;
+};
+
+/**
+ * \brief Class to not load any tile specific data when iterating over changes.
+ */
+class NoTileData : AbstractTileData {
+ public:
+  NoTileData(Image *UNUSED(image), ImageUser *UNUSED(image_user))
+  {
+  }
+
+  void init_data(TileNumber UNUSED(new_tile_number)) override
+  {
+  }
+
+  void free_data() override
+  {
+  }
+};
+
+/**
+ * \brief Load the ImageTile and ImBuf associated with the partial change.
+ */
+class ImageTileData : AbstractTileData {
+ public:
+  /**
+   * \brief Not owned Image that is being iterated over.
+   */
+  Image *image;
+
+  /**
+   * \brief Local copy of the image user.
+   *
+   * The local copy is required so we don't change the image user of the caller.
+   * We need to change it in order to request data for a specific tile.
+   */
+  ImageUser image_user = {0};
+
+  /**
+   * \brief ImageTile associated with the loaded tile.
+   * Data is not owned by this instance but by the `image`.
+   */
+  ImageTile *tile = nullptr;
+
+  /**
+   * \brief ImBuf of the loaded tile.
+   *
+   * Can be nullptr when the file doesn't exist or when the tile hasn't been initialized.
+   */
+  ImBuf *tile_buffer = nullptr;
+
+  ImageTileData(Image *image, ImageUser *image_user) : image(image)
+  {
+    if (image_user != nullptr) {
+      this->image_user = *image_user;
+    }
+  }
+
+  void init_data(TileNumber new_tile_number) override
+  {
+    image_user.tile = new_tile_number;
+    tile = BKE_image_get_tile(image, new_tile_number);
+    tile_buffer = BKE_image_acquire_ibuf(image, &image_user, NULL);
+  }
+
+  void free_data() override
+  {
+    BKE_image_release_ibuf(image, tile_buffer, nullptr);
+    tile = nullptr;
+    tile_buffer = nullptr;
+  }
+};
+
+template<typename TileData = NoTileData> struct PartialUpdateChecker {
+
+  /**
+   * \brief Not owned Image that is being iterated over.
+   */
+  Image *image;
+  ImageUser *image_user;
+
+  /**
+   * \brief the collected changes are stored inside the PartialUpdateUser.
+   */
+  PartialUpdateUser *user;
+
+  struct CollectResult {
+    PartialUpdateChecker<TileData> *checker;
+
+    /**
+     * \brief Tile specific data.
+     */
+    TileData tile_data;
+    PartialUpdateRegion changed_region;
+    ePartialUpdateCollectResult result_code;
+
+   private:
+    TileNumber last_tile_number;
+
+   public:
+    CollectResult(PartialUpdateChecker<TileData> *checker, ePartialUpdateCollect

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list