[Bf-blender-cvs] [32c90d2d7c9] master: Cleanup: Split image engine into ImageEngine, SpaceAccessor and DrawingMode.

Jeroen Bakker noreply at git.blender.org
Fri Nov 5 13:30:39 CET 2021


Commit: 32c90d2d7c96225f3271fc5e9bc64f7ad81b886f
Author: Jeroen Bakker
Date:   Fri Nov 5 13:25:00 2021 +0100
Branches: master
https://developer.blender.org/rB32c90d2d7c96225f3271fc5e9bc64f7ad81b886f

Cleanup: Split image engine into ImageEngine, SpaceAccessor and DrawingMode.

Image engine is used to draw an image into a space. The current
structure wasn't clear and couldn't be easilly extended. This refactor
spliced the image draw engine into 3 main components.

- Space accessors: contains an interface to communicate with space data
  (Image editor, UV Editor, Node Editor) in a common way. This reduced
  the branching in the code base.
- DrawingMode: contains an interface to the used tactic to draw an image
  inside the space framebuffer. Currently only one mode is implemented;
  in the future there could be a separate drawing mode for huge images.
- ImageEngine: the core that connects the draw manager with the space
  data and drawing mode.

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

A	source/blender/draw/engines/image/image_drawing_mode.hh
M	source/blender/draw/engines/image/image_engine.cc
M	source/blender/draw/engines/image/image_engine.h
M	source/blender/draw/engines/image/image_private.hh
A	source/blender/draw/engines/image/image_space_image.hh
A	source/blender/draw/engines/image/image_space_node.hh

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

diff --git a/source/blender/draw/engines/image/image_drawing_mode.hh b/source/blender/draw/engines/image/image_drawing_mode.hh
new file mode 100644
index 00000000000..f2f5bbe554c
--- /dev/null
+++ b/source/blender/draw/engines/image/image_drawing_mode.hh
@@ -0,0 +1,154 @@
+/*
+ * 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 draw_engine
+ */
+
+#pragma once
+
+#include "image_private.hh"
+
+namespace blender::draw::image_engine {
+
+class DefaultDrawingMode : public AbstractDrawingMode {
+ private:
+  DRWPass *create_image_pass() const
+  {
+    /* Write depth is needed for background overlay rendering. Near depth is used for
+     * transparency checker and Far depth is used for indicating the image size. */
+    DRWState state = static_cast<DRWState>(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
+                                           DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND_ALPHA_PREMUL);
+    return DRW_pass_create("Image", state);
+  }
+
+  void add_to_shgroup(AbstractSpaceAccessor *space,
+                      DRWShadingGroup *grp,
+                      const Image *image,
+                      const ImBuf *image_buffer) const
+  {
+    float image_mat[4][4];
+
+    const DRWContextState *draw_ctx = DRW_context_state_get();
+    const ARegion *region = draw_ctx->region;
+    space->get_image_mat(image_buffer, region, image_mat);
+
+    GPUBatch *geom = DRW_cache_quad_get();
+
+    const bool is_tiled_texture = image && image->source == IMA_SRC_TILED;
+    if (is_tiled_texture) {
+      const float translate_x = image_mat[3][0];
+      const float translate_y = image_mat[3][1];
+      LISTBASE_FOREACH (ImageTile *, tile, &image->tiles) {
+        const int tile_x = ((tile->tile_number - 1001) % 10);
+        const int tile_y = ((tile->tile_number - 1001) / 10);
+        image_mat[3][0] = (float)tile_x + translate_x;
+        image_mat[3][1] = (float)tile_y + translate_y;
+        DRW_shgroup_call_obmat(grp, geom, image_mat);
+      }
+    }
+    else {
+      DRW_shgroup_call_obmat(grp, geom, image_mat);
+    }
+  }
+
+ public:
+  void cache_init(IMAGE_Data *vedata) const override
+  {
+    IMAGE_PassList *psl = vedata->psl;
+
+    psl->image_pass = create_image_pass();
+  }
+
+  void cache_image(AbstractSpaceAccessor *space,
+                   IMAGE_Data *vedata,
+                   Image *image,
+                   ImageUser *iuser,
+                   ImBuf *image_buffer) const override
+  {
+    IMAGE_PassList *psl = vedata->psl;
+    IMAGE_StorageList *stl = vedata->stl;
+    IMAGE_PrivateData *pd = stl->pd;
+
+    GPUTexture *tex_tile_data = nullptr;
+    space->get_gpu_textures(
+        image, iuser, image_buffer, &pd->texture, &pd->owns_texture, &tex_tile_data);
+    if (pd->texture == nullptr) {
+      return;
+    }
+    const bool is_tiled_texture = tex_tile_data != nullptr;
+
+    ShaderParameters sh_params;
+    sh_params.use_premul_alpha = BKE_image_has_gpu_texture_premultiplied_alpha(image,
+                                                                               image_buffer);
+    const DRWContextState *draw_ctx = DRW_context_state_get();
+    const Scene *scene = draw_ctx->scene;
+    if (scene->camera && scene->camera->type == OB_CAMERA) {
+      Camera *camera = static_cast<Camera *>(scene->camera->data);
+      copy_v2_fl2(sh_params.far_near, camera->clip_end, camera->clip_start);
+    }
+    space->get_shader_parameters(sh_params, image_buffer, is_tiled_texture);
+
+    GPUShader *shader = IMAGE_shader_image_get(is_tiled_texture);
+    DRWShadingGroup *shgrp = DRW_shgroup_create(shader, psl->image_pass);
+    if (is_tiled_texture) {
+      DRW_shgroup_uniform_texture_ex(shgrp, "imageTileArray", pd->texture, GPU_SAMPLER_DEFAULT);
+      DRW_shgroup_uniform_texture(shgrp, "imageTileData", tex_tile_data);
+    }
+    else {
+      DRW_shgroup_uniform_texture_ex(shgrp, "imageTexture", pd->texture, GPU_SAMPLER_DEFAULT);
+    }
+    DRW_shgroup_uniform_vec2_copy(shgrp, "farNearDistances", sh_params.far_near);
+    DRW_shgroup_uniform_vec4_copy(shgrp, "color", ShaderParameters::color);
+    DRW_shgroup_uniform_vec4_copy(shgrp, "shuffle", sh_params.shuffle);
+    DRW_shgroup_uniform_int_copy(shgrp, "drawFlags", sh_params.flags);
+    DRW_shgroup_uniform_bool_copy(shgrp, "imgPremultiplied", sh_params.use_premul_alpha);
+
+    add_to_shgroup(space, shgrp, image, image_buffer);
+  }
+
+  void draw_finish(IMAGE_Data *vedata) const override
+  {
+    IMAGE_StorageList *stl = vedata->stl;
+    IMAGE_PrivateData *pd = stl->pd;
+
+    if (pd->texture && pd->owns_texture) {
+      GPU_texture_free(pd->texture);
+      pd->owns_texture = false;
+    }
+    pd->texture = nullptr;
+  }
+
+  void draw_scene(IMAGE_Data *vedata) const override
+  {
+    IMAGE_PassList *psl = vedata->psl;
+    IMAGE_PrivateData *pd = vedata->stl->pd;
+
+    DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
+    GPU_framebuffer_bind(dfbl->default_fb);
+    static float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+    GPU_framebuffer_clear_color_depth(dfbl->default_fb, clear_col, 1.0);
+
+    DRW_view_set_active(pd->view);
+    DRW_draw_pass(psl->image_pass);
+    DRW_view_set_active(nullptr);
+  }
+};
+
+}  // namespace blender::draw::image_engine
+
diff --git a/source/blender/draw/engines/image/image_engine.cc b/source/blender/draw/engines/image/image_engine.cc
index 776639f5224..6ccf20ab339 100644
--- a/source/blender/draw/engines/image/image_engine.cc
+++ b/source/blender/draw/engines/image/image_engine.cc
@@ -17,13 +17,16 @@
  */
 
 /** \file
- * \ingroup draw_editors
+ * \ingroup draw_engine
  *
  * Draw engine to draw the Image/UV editor
  */
 
 #include "DRW_render.h"
 
+#include <memory>
+#include <optional>
+
 #include "BKE_image.h"
 #include "BKE_main.h"
 #include "BKE_object.h"
@@ -38,291 +41,89 @@
 
 #include "GPU_batch.h"
 
+#include "image_drawing_mode.hh"
 #include "image_engine.h"
 #include "image_private.hh"
+#include "image_space_image.hh"
+#include "image_space_node.hh"
 
 namespace blender::draw::image_engine {
 
-#define IMAGE_DRAW_FLAG_SHOW_ALPHA (1 << 0)
-#define IMAGE_DRAW_FLAG_APPLY_ALPHA (1 << 1)
-#define IMAGE_DRAW_FLAG_SHUFFLING (1 << 2)
-#define IMAGE_DRAW_FLAG_DEPTH (1 << 3)
-#define IMAGE_DRAW_FLAG_DO_REPEAT (1 << 4)
-#define IMAGE_DRAW_FLAG_USE_WORLD_POS (1 << 5)
-
-static void image_cache_image_add(DRWShadingGroup *grp, Image *image, ImBuf *ibuf)
+static std::unique_ptr<AbstractSpaceAccessor> space_accessor_from_context(
+    const DRWContextState *draw_ctx)
 {
-  const DRWContextState *draw_ctx = DRW_context_state_get();
-  const ARegion *region = draw_ctx->region;
   const char space_type = draw_ctx->space_data->spacetype;
-
-  float zoom_x = 1.0f;
-  float zoom_y = 1.0f;
-  float translate_x = 0.0f;
-  float translate_y = 0.0f;
-
-  /* User can freely move the backdrop in the space of the node editor */
-  if (space_type == SPACE_NODE) {
-    SpaceNode *snode = (SpaceNode *)draw_ctx->space_data;
-    const float ibuf_width = ibuf->x;
-    const float ibuf_height = ibuf->y;
-    const float x = (region->winx - snode->zoom * ibuf_width) / 2 + snode->xof;
-    const float y = (region->winy - snode->zoom * ibuf_height) / 2 + snode->yof;
-
-    zoom_x = ibuf_width * snode->zoom;
-    zoom_y = ibuf_height * snode->zoom;
-    translate_x = x;
-    translate_y = y;
-  }
-
-  const bool is_tiled_texture = image && image->source == IMA_SRC_TILED;
-  float obmat[4][4];
-  unit_m4(obmat);
-
-  GPUBatch *geom = DRW_cache_quad_get();
-
-  obmat[0][0] = zoom_x;
-  obmat[1][1] = zoom_y;
-  obmat[3][1] = translate_y;
-  obmat[3][0] = translate_x;
-
-  if (is_tiled_texture) {
-    LISTBASE_FOREACH (ImageTile *, tile, &image->tiles) {
-      const int tile_x = ((tile->tile_number - 1001) % 10);
-      const int tile_y = ((tile->tile_number - 1001) / 10);
-      obmat[3][1] = (float)tile_y + translate_y;
-      obmat[3][0] = (float)tile_x + translate_x;
-      DRW_shgroup_call_obmat(grp, geom, obmat);
-    }
+  if (space_type == SPACE_IMAGE) {
+    return std::make_unique<SpaceImageAccessor>((SpaceImage *)draw_ctx->space_data);
   }
-  else {
-    DRW_shgroup_call_obmat(grp, geom, obmat);
+  if (space_type == SPACE_NODE) {
+    return std::make_unique<SpaceNodeAccessor>((SpaceNode *)draw_ctx->space_data);
   }
+  BLI_assert_unreachable();
+  return nullptr;
 }
 
-static void space_image_gpu_texture_get(Image *image,
-                                        ImageUser *iuser,
-                                        ImBuf *ibuf,
-                                        GPUTexture **r_gpu_texture,
-                                        bool *r_owns_texture,
-                                        GPUTexture **r_tex_tile_data)
-{
-  const DRWContextState *draw_ctx = DRW_context_state_get();
-  SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
-  if (image->rr != nullptr) {
-    /* Update multi-index and pass for the current eye. */
-    BKE_image_multilayer_index(image->rr, &sima->iuser);
-  }
-  else {
-    BKE_image_multiview_index(image, &sima->iuser);
-  }
+class ImageEngine {
+ private:
+  const DRWContextState *draw_ctx;
+  IMAGE_Data *vedata;
+  std::unique_ptr<AbstractSpaceAccessor> space;
+  DefaultDrawingMode drawing_mode;
 
-  if (ibuf == nullptr) {
-    return;
+ public:
+  ImageEngine(const DRWContextState *draw_ctx, IMAGE_Data *vedata)
+      : draw_ctx(draw_ctx), vedata(vedata), space(space_accessor_from_context(draw_ctx))
+  {
   }
 
-  if (ibuf->rect

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list